﻿'''' Last Modified on Dec 15, 2014 09.35 am.

Imports System.Data
Imports System.Data.OleDb
Imports ADOEntityKYC
Imports System.Data.SqlClient

Partial Class KYC_NonIndividualCorp
    Inherits System.Web.UI.Page

#Region "Variable"

    Dim dtAcc1 As New DataTable
    Dim dtAcc21 As New DataTable
    Dim dtAcc22 As New DataTable
    Dim dtAcc3 As New DataTable
    Dim dtAcc4 As New DataTable
    Dim dtAcc4_Del As New DataTable
    Dim dtAcc5 As New DataTable
    Dim indexGridView1 As Int32 = -1
    Dim indexGridView21 As Int32 = -1
    Dim indexGridView22 As Int32 = -1
    Dim indexGridView3 As Int32 = -1
    Dim indexGridView4 As Int32 = -1
    Dim indexGridView5 As Int32 = -1
    Dim clsValid As clsValidateData
    Dim clsDb As New clsDatabase
    Dim lvsSQL As String = ""
    Dim clsDatabase As clsDatabase
    Dim clsSessionMenuRight As clsSessionMenuRight = New clsSessionMenuRight
    Dim Position_11 As String = "เจ้าของ"
    Dim Position_21 As String = "กรรมการ"
    Dim Position_22 As String = "กรรมการที่มีอำนาจลงนามผูกพันนิติบุคคล"
    Dim Position_31 As String = "ผู้ถือหุ้นบุคคลธรรมดาตั้งแต่ร้อยละ 20 ขึ้นไป"
    Dim Position_41 As String = "ผู้ถือหุ้นนิติบุคคลตั้งแต่ร้อยละ 20 ขึ้นไป"
    Dim Position_51 As String = "ผู้รับมอบอำนาจให้สร้างความสัมพันธ์กับธนาคาร"
    ''''
    Dim clsCheckID As clsCheckID

#End Region

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        clsValid = New clsValidateData()
        clsDatabase = New clsDatabase

        Try
            btnSave.Attributes.Add("onclick", "NoDoubleSave();")
            btnSancList_A.Attributes.Add("onclick", "NoDoubleSubmit(); setParam_SEARCH_A(); popAMListWindow();")
            btnSancList_S.Attributes.Add("onclick", "NoDoubleSubmit(); setParam_SEARCH_S(); popAMListWindow();")
            btnSancList_D.Attributes.Add("onclick", "NoDoubleSubmit(); setParam_SEARCH_D(); popAMListWindow();")
            btnSancList_F.Attributes.Add("onclick", "NoDoubleSubmit(); setParam_SEARCH_F(); popAMListWindow();")
            btnSancList_H.Attributes.Add("onclick", "NoDoubleSubmit(); setParam_SEARCH_H(); popAMListWindow();")

            Me.btnClose.Attributes.Add("onclick", "Close()")
            If Not IsPostBack Then
                Me.loadDataToDropDown()
                Set_StartPage()
                BindData(1, 0)
            End If

        Catch ex As Exception
            'ScriptManager.RegisterStartupScript(UpdatePanel1, _
            '                                                UpdatePanel1.GetType(), _
            '                                                "clientScript", _
            '                                                "alert('" + ex.Message.Replace("'", "") + "')", _
            '                                                True)
        End Try

    End Sub

    Private Sub ClearFieldA()

        Me.txtINDV_FIRST_NAME_A.ReadOnly = False
        Me.txtINDV_LAST_NAME_A.ReadOnly = False
        Me.txtUNIQUEID_A.ReadOnly = False

        Me.ddlPrefixTH_A.SelectedIndex = -1

        txtINDV_FIRST_NAME_A.Text = ""
        txtINDV_LAST_NAME_A.Text = ""
        txtUNIQUEID_A.Text = ""
        lblSanc_Result_A.Text = ""
        lblRisk_Level_A.Text = ""
        hidIndexA.Value = ""
        GridView1.SelectedIndex = -1

        btnAddA.Visible = True
        btnEditA.Visible = False
    End Sub

    Private Sub ClearFieldS()

        Me.ddlPrefixTH_S.SelectedIndex = -1

        txtINDV_FIRST_NAME_S.Text = ""
        txtINDV_LAST_NAME_S.Text = ""
        txtUNIQUEID_S.Text = ""
        lblSanc_Result_S.Text = ""
        lblRisk_Level_S.Text = ""
        hidIndexS.Value = ""
        GridView21.SelectedIndex = -1

        btnAddS.Visible = True
        btnEditS.Visible = False
    End Sub

    Private Sub ClearFieldD()

        Me.ddlPrefixTH_D.SelectedIndex = -1

        txtINDV_FIRST_NAME_D.Text = ""
        txtINDV_LAST_NAME_D.Text = ""
        txtUNIQUEID_D.Text = ""
        lblSanc_Result_D.Text = ""
        lblRisk_Level_D.Text = ""
        hidIndexD.Value = ""
        GridView22.SelectedIndex = -1

        btnAddD.Visible = True
        btnEditD.Visible = False
    End Sub

    Private Sub ClearFieldF()

        Me.ddlPrefixTH_F.SelectedIndex = -1

        txtINDV_FIRST_NAME_F.Text = ""
        txtINDV_LAST_NAME_F.Text = ""
        txtUNIQUEID_F.Text = ""
        hidIndexF.Value = ""
        lblSanc_Result_F.Text = ""
        lblRisk_Level_F.Text = ""
        GridView3.SelectedIndex = -1

        btnAddF.Visible = True
        btnEditF.Visible = False
    End Sub

    Private Sub ClearFieldG()
        txtCORP_ID_SHR_HOLDER.Text = ""
        txtCORP_ID_SHR_HOLDER_NAME.Text = ""
        'txtUNIQUEID_G.Text = ""
        hidIndexG.Value = ""
        hidSeqG.Value = ""
        GridView4.SelectedIndex = -1

        btnAddG.Visible = True
        btnEditG.Visible = False
        btnAddSub_Corp.Visible = False
    End Sub

    Private Sub ClearFieldH()

        Me.ddlPrefixTH_H.SelectedIndex = -1

        txtINDV_FIRST_NAME_H.Text = ""
        txtINDV_LAST_NAME_H.Text = ""
        txtUNIQUEID_H.Text = ""
        hidIndexH.Value = ""
        Me.txtINDV_CONNECTED_TO_CORP_H.Text = ""
        Me.txtINDV_COMPLETED_H.Text = ""
        lblSanc_Result_H.Text = ""
        lblRisk_Level_H.Text = ""
        GridView5.SelectedIndex = -1

        btnAddH.Visible = True
        btnEditH.Visible = False
    End Sub

    Private Sub ClearPage()
        txtCORP_ID.Text = ""
        txtCORP_NAME.Text = ""
        txtOWNER_CORP_ID.Text = ""
        txtOWNER_CORP_NAME.Text = ""
        txtCORP_LEVEL.Text = ""
        txtCORP_SEQ.Text = ""
        hidHaveEdit.Value = "0"
        lblSave.Visible = False

        ClearFieldA()
        ClearFieldS()
        ClearFieldD()
        ClearFieldF()
        ClearFieldG()
        ClearFieldH()

        Session("A") = Nothing
        Session("S") = Nothing
        Session("D") = Nothing
        Session("F") = Nothing
        Session("G") = Nothing
        Session("H") = Nothing

        GridView1.DataSource = dtAcc1
        GridView1.DataBind() : Me.lblCount_GridView1.Text = dtAcc1.Rows.Count
        GridView21.DataSource = dtAcc21
        GridView21.DataBind() : Me.lblCount_GridView21.Text = dtAcc21.Rows.Count
        GridView22.DataSource = dtAcc22
        GridView22.DataBind() : Me.lblCount_GridView22.Text = dtAcc22.Rows.Count
        GridView3.DataSource = dtAcc3
        GridView3.DataBind() : Me.lblCount_ShrHolder_INDV.Text = dtAcc3.Rows.Count
        GridView4.DataSource = dtAcc4
        GridView4.DataBind() : Me.lblCount_GridView4.Text = dtAcc4.Rows.Count
        GridView5.DataSource = dtAcc5
        GridView5.DataBind() : Me.lblCount_GridView5.Text = dtAcc5.Rows.Count
    End Sub

    ' ------- รายชื่อเจ้าของ -------
    Protected Sub btnAddA_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAddA.Click
        Dim dRow As DataRow

        ''''''Validate before add row''''''''
        If Validate_A() = False Then Exit Sub
        ''''''''''''''''''''''''''''''''''''''

        If (Session("A") Is Nothing) Then
            dtAcc1.Columns.Add(New DataColumn("GROUP_CODE"))
            dtAcc1.Columns.Add(New DataColumn("GROUP_NAME"))
            dtAcc1.Columns.Add(New DataColumn("GROUP_CODE_SUB"))
            dtAcc1.Columns.Add(New DataColumn("GROUP_CODE_SUB_NAME"))
            dtAcc1.Columns.Add(New DataColumn("GROUP_CODE_SUB_SEQ"))
            dtAcc1.Columns.Add(New DataColumn("THPREFIX"))
            dtAcc1.Columns.Add(New DataColumn("THPREFIX_DESC"))
            dtAcc1.Columns.Add(New DataColumn("INDV_FIRST_NAME"))
            dtAcc1.Columns.Add(New DataColumn("INDV_LAST_NAME"))
            dtAcc1.Columns.Add(New DataColumn("SEARCH_RESULT"))
            dtAcc1.Columns.Add(New DataColumn("RISK_LEVEL"))
            dtAcc1.Columns.Add(New DataColumn("UNIQUEID"))
        Else
            dtAcc1 = Session("A")
        End If

        dRow = dtAcc1.NewRow()
        dRow("GROUP_CODE") = System.Convert.ToInt32(lblGroupCode_1.Text)
        dRow("GROUP_NAME") = lblGroupCodeName_1.Text
        dRow("GROUP_CODE_SUB") = System.Convert.ToInt32(lblGroupCodeSub_11.Text)
        dRow("GROUP_CODE_SUB_NAME") = Position_11
        dRow("GROUP_CODE_SUB_SEQ") = GridView1.Rows.Count + 1
        dRow("THPREFIX") = Me.ddlPrefixTH_A.SelectedIndex
        dRow("THPREFIX_DESC") = Me.ddlPrefixTH_A.SelectedItem
        dRow("INDV_FIRST_NAME") = txtINDV_FIRST_NAME_A.Text
        dRow("INDV_LAST_NAME") = txtINDV_LAST_NAME_A.Text
        dRow("SEARCH_RESULT") = Me.lblSanc_Result_A.Text
        dRow("RISK_LEVEL") = CInt(Me.lblRisk_Level_A.Text)
        dRow("UNIQUEID") = txtUNIQUEID_A.Text
        dtAcc1.Rows.Add(dRow)
        dtAcc1.AcceptChanges()

        GridView1.DataSource = dtAcc1
        GridView1.DataBind()
        Me.lblCount_GridView1.Text = dtAcc1.Rows.Count
        hidHaveEdit.Value = "1"
        Session("A") = dtAcc1
        ClearFieldA()
    End Sub

    Protected Sub btnEditA_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnEditA.Click
        Dim index_row As Integer = System.Convert.ToInt32(hidIndexA.Value)

        If clsValid.CheckNull(Me.txtINDV_FIRST_NAME_A) = False Then Exit Sub
        If clsValid.CheckNull(Me.txtINDV_LAST_NAME_A) = False Then Exit Sub
        If clsValid.CheckNull(Me.txtUNIQUEID_A) = False Then Exit Sub
        If Me.Validate_ID_CARD(Me.txtUNIQUEID_A) = False Then Exit Sub

        If Me.lblSanc_Result_A.Text = "" Then
            ScriptManager.RegisterStartupScript(Page, Page.GetType(), "alertMessage", "window.alert('กรุณาตรวจกรองรายชื่อก่อน');", True)
            Me.btnSancList_A.Focus()
            Exit Sub
        End If
        '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        dtAcc1 = Session("A")
        dtAcc1(index_row)("THPREFIX") = Me.ddlPrefixTH_A.SelectedIndex
        dtAcc1(index_row)("THPREFIX_DESC") = Me.ddlPrefixTH_A.SelectedItem
        dtAcc1(index_row)("INDV_FIRST_NAME") = txtINDV_FIRST_NAME_A.Text
        dtAcc1(index_row)("INDV_LAST_NAME") = txtINDV_LAST_NAME_A.Text
        dtAcc1(index_row)("UNIQUEID") = txtUNIQUEID_A.Text
        GridView1.DataSource = dtAcc1
        GridView1.DataBind()
        Me.lblCount_GridView1.Text = dtAcc1.Rows.Count

        hidHaveEdit.Value = "1"
        Session("A") = dtAcc1
        ClearFieldA()
    End Sub

    Protected Sub btnDelA_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDelA.Click
        Try
            Dim index As Integer = GridView1.SelectedIndex
            If index = -1 Then
                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "clientScript", "alert('ต้องเลือกรายการที่ต้องการลบก่อน')", True)
            Else
                dtAcc1 = Session("A")
                dtAcc1.Rows.RemoveAt(index)
                GridView1.DataSource = dtAcc1
                GridView1.DataBind()
                Me.lblCount_GridView1.Text = dtAcc1.Rows.Count

                Session("A") = dtAcc1
                ClearFieldA()
            End If

        Catch ex As Exception
            ScriptManager.RegisterStartupScript(Page, _
                                                            Page.GetType(), _
                                                            "clientScript", _
                                                            "alert('" + ex.Message.Replace("'", "") + "')", _
                                                            True)
        End Try
    End Sub

    Protected Sub GridView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.SelectedIndexChanged
        If GridView1.SelectedIndex = indexGridView1 Then
            ClearFieldA()
        Else
            ddlPrefixTH_A.SelectedIndex = CType(GridView1.Rows(GridView1.SelectedIndex).FindControl("lblGrid_THPREFIX_A"), Label).Text
            txtINDV_FIRST_NAME_A.Text = CType(GridView1.Rows(GridView1.SelectedIndex).FindControl("lblGrid_INDV_FIRST_NAME_A"), Label).Text
            txtINDV_LAST_NAME_A.Text = CType(GridView1.Rows(GridView1.SelectedIndex).FindControl("lblGrid_INDV_LAST_NAME_A"), Label).Text
            txtUNIQUEID_A.Text = CType(GridView1.Rows(GridView1.SelectedIndex).FindControl("lblGrid_UNIQUEID_A"), Label).Text

            hidIndexA.Value = GridView1.SelectedIndex.ToString()
            btnAddA.Visible = False
            btnEditA.Visible = True
        End If
    End Sub

    Protected Sub GridView1_SelectedIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSelectEventArgs) Handles GridView1.SelectedIndexChanging
        indexGridView1 = GridView1.SelectedIndex
    End Sub

    ' ------- รายชื่อกรรมการที่มีอำนาจลงนามผูกพันนิติบุคคล -------
    Protected Sub btnAddS_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAddS.Click
        Dim dRow As DataRow

        ''''''Validate before add row''''''''
        If Validate_S() = False Then Exit Sub
        ''''''''''''''''''''''''''''''''''''''

        If (Session("S") Is Nothing) Then
            dtAcc21.Columns.Add(New DataColumn("GROUP_CODE"))
            dtAcc21.Columns.Add(New DataColumn("GROUP_NAME"))
            dtAcc21.Columns.Add(New DataColumn("GROUP_CODE_SUB"))
            dtAcc21.Columns.Add(New DataColumn("GROUP_CODE_SUB_NAME"))
            dtAcc21.Columns.Add(New DataColumn("GROUP_CODE_SUB_SEQ"))
            dtAcc21.Columns.Add(New DataColumn("THPREFIX"))
            dtAcc21.Columns.Add(New DataColumn("THPREFIX_DESC"))
            dtAcc21.Columns.Add(New DataColumn("INDV_FIRST_NAME"))
            dtAcc21.Columns.Add(New DataColumn("INDV_LAST_NAME"))
            dtAcc21.Columns.Add(New DataColumn("SEARCH_RESULT"))
            dtAcc21.Columns.Add(New DataColumn("RISK_LEVEL"))
            dtAcc21.Columns.Add(New DataColumn("UNIQUEID"))
        Else
            dtAcc21 = Session("S")
        End If

        dRow = dtAcc21.NewRow()
        dRow("GROUP_CODE") = System.Convert.ToInt32(lblGroupCode_2.Text)
        dRow("GROUP_NAME") = lblGroupCodeName_2.Text
        dRow("GROUP_CODE_SUB") = System.Convert.ToInt32(lblGroupCodeSub_21.Text)
        dRow("GROUP_CODE_SUB_NAME") = Position_21
        dRow("GROUP_CODE_SUB_SEQ") = GridView21.Rows.Count + 1
        dRow("THPREFIX") = Me.ddlPrefixTH_S.SelectedIndex
        dRow("THPREFIX_DESC") = Me.ddlPrefixTH_S.SelectedItem
        dRow("INDV_FIRST_NAME") = txtINDV_FIRST_NAME_S.Text
        dRow("INDV_LAST_NAME") = txtINDV_LAST_NAME_S.Text
        dRow("SEARCH_RESULT") = Me.lblSanc_Result_S.Text
        dRow("RISK_LEVEL") = Me.lblRisk_Level_S.Text
        dRow("UNIQUEID") = txtUNIQUEID_S.Text
        dtAcc21.Rows.Add(dRow)
        dtAcc21.AcceptChanges()

        GridView21.DataSource = dtAcc21
        GridView21.DataBind()
        Me.lblCount_GridView21.Text = dtAcc21.Rows.Count
        hidHaveEdit.Value = "1"
        Session("S") = dtAcc21
        ClearFieldS()
    End Sub

    Protected Sub btnEditS_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnEditS.Click
        Dim index_row As Integer = System.Convert.ToInt32(hidIndexS.Value)
        dtAcc21 = Session("S")
        dtAcc21(index_row)("THPREFIX") = Me.ddlPrefixTH_S.SelectedIndex
        dtAcc21(index_row)("THPREFIX_DESC") = Me.ddlPrefixTH_S.SelectedItem
        dtAcc21(index_row)("INDV_FIRST_NAME") = txtINDV_FIRST_NAME_S.Text
        dtAcc21(index_row)("INDV_LAST_NAME") = txtINDV_LAST_NAME_S.Text
        dtAcc21(index_row)("UNIQUEID") = txtUNIQUEID_S.Text
        GridView21.DataSource = dtAcc21
        GridView21.DataBind()
        Me.lblCount_GridView21.Text = dtAcc21.Rows.Count

        hidHaveEdit.Value = "1"
        Session("S") = dtAcc21
        ClearFieldS()
    End Sub

    Protected Sub btnDelS_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDelS.Click
        Try
            Dim index As Integer = GridView21.SelectedIndex
            If index = -1 Then
                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "clientScript", "alert('ต้องเลือกรายการที่ต้องการลบก่อน')", True)
            Else
                dtAcc21 = Session("S")
                dtAcc21.Rows.RemoveAt(index)
                GridView21.DataSource = dtAcc21
                GridView21.DataBind()
                Me.lblCount_GridView21.Text = dtAcc21.Rows.Count
                Session("S") = dtAcc21
                ClearFieldS()
            End If

        Catch ex As Exception
            ScriptManager.RegisterStartupScript(Page, _
                                                            Page.GetType(), _
                                                            "clientScript", _
                                                            "alert('" + ex.Message.Replace("'", "") + "')", _
                                                            True)
        End Try
    End Sub

    Protected Sub GridView21_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView21.SelectedIndexChanged
        If GridView21.SelectedIndex = indexGridView21 Then
            ClearFieldS()
        Else
            ddlPrefixTH_S.SelectedIndex = CType(GridView21.Rows(GridView21.SelectedIndex).FindControl("lblGrid_THPREFIX_S"), Label).Text
            txtINDV_FIRST_NAME_S.Text = CType(GridView21.Rows(GridView21.SelectedIndex).FindControl("lblGrid_INDV_FIRST_NAME_S"), Label).Text
            txtINDV_LAST_NAME_S.Text = CType(GridView21.Rows(GridView21.SelectedIndex).FindControl("lblGrid_INDV_LAST_NAME_S"), Label).Text
            txtUNIQUEID_S.Text = CType(GridView21.Rows(GridView21.SelectedIndex).FindControl("lblGrid_UNIQUEID_S"), Label).Text

            hidIndexS.Value = GridView21.SelectedIndex.ToString()
            btnAddS.Visible = False
            btnEditS.Visible = True
        End If
    End Sub

    Protected Sub GridView21_SelectedIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSelectEventArgs) Handles GridView21.SelectedIndexChanging
        indexGridView21 = GridView21.SelectedIndex
    End Sub

    ' ------- รายชื่อกรรมการที่ไม่มีอำนาจลงนามผูกพันนิติบุคคล -------
    Protected Sub btnAddD_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAddD.Click
        Dim dRow As DataRow

        ''''''Validate before add row''''''''
        If Validate_D() = False Then Exit Sub
        ''''''''''''''''''''''''''''''''''''''

        If (Session("D") Is Nothing) Then
            dtAcc22.Columns.Add(New DataColumn("GROUP_CODE"))
            dtAcc22.Columns.Add(New DataColumn("GROUP_NAME"))
            dtAcc22.Columns.Add(New DataColumn("GROUP_CODE_SUB"))
            dtAcc22.Columns.Add(New DataColumn("GROUP_CODE_SUB_NAME"))
            dtAcc22.Columns.Add(New DataColumn("GROUP_CODE_SUB_SEQ"))
            dtAcc22.Columns.Add(New DataColumn("THPREFIX"))
            dtAcc22.Columns.Add(New DataColumn("THPREFIX_DESC"))
            dtAcc22.Columns.Add(New DataColumn("INDV_FIRST_NAME"))
            dtAcc22.Columns.Add(New DataColumn("INDV_LAST_NAME"))
            dtAcc22.Columns.Add(New DataColumn("SEARCH_RESULT"))
            dtAcc22.Columns.Add(New DataColumn("RISK_LEVEL"))
            dtAcc22.Columns.Add(New DataColumn("UNIQUEID"))
        Else
            dtAcc22 = Session("D")
        End If

        dRow = dtAcc22.NewRow()
        dRow("GROUP_CODE") = System.Convert.ToInt32(lblGroupCode_2.Text)
        dRow("GROUP_NAME") = lblGroupCodeName_2.Text
        dRow("GROUP_CODE_SUB") = System.Convert.ToInt32(lblGroupCodeSub_22.Text)
        dRow("GROUP_CODE_SUB_NAME") = Position_22
        dRow("GROUP_CODE_SUB_SEQ") = GridView22.Rows.Count + 1
        dRow("THPREFIX") = Me.ddlPrefixTH_D.SelectedIndex
        dRow("THPREFIX_DESC") = Me.ddlPrefixTH_D.SelectedItem
        dRow("INDV_FIRST_NAME") = txtINDV_FIRST_NAME_D.Text
        dRow("INDV_LAST_NAME") = txtINDV_LAST_NAME_D.Text
        dRow("SEARCH_RESULT") = Me.lblSanc_Result_D.Text
        dRow("RISK_LEVEL") = Me.lblRisk_Level_D.Text
        dRow("UNIQUEID") = txtUNIQUEID_D.Text
        dtAcc22.Rows.Add(dRow)
        dtAcc22.AcceptChanges()

        GridView22.DataSource = dtAcc22
        GridView22.DataBind() : Me.lblCount_GridView22.Text = dtAcc22.Rows.Count
        hidHaveEdit.Value = "1"
        Session("D") = dtAcc22
        ClearFieldD()
    End Sub

    Protected Sub btnEditD_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnEditD.Click
        Dim index_row As Integer = System.Convert.ToInt32(hidIndexD.Value)
        dtAcc22 = Session("D")
        dtAcc22(index_row)("THPREFIX") = Me.ddlPrefixTH_D.SelectedIndex
        dtAcc22(index_row)("THPREFIX_DESC") = Me.ddlPrefixTH_D.SelectedItem
        dtAcc22(index_row)("INDV_FIRST_NAME") = txtINDV_FIRST_NAME_D.Text
        dtAcc22(index_row)("INDV_LAST_NAME") = txtINDV_LAST_NAME_D.Text
        dtAcc22(index_row)("UNIQUEID") = txtUNIQUEID_D.Text
        GridView22.DataSource = dtAcc22
        GridView22.DataBind() : Me.lblCount_GridView22.Text = dtAcc22.Rows.Count

        hidHaveEdit.Value = "1"
        Session("D") = dtAcc22
        ClearFieldD()
    End Sub

    Protected Sub btnDelD_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDelD.Click
        Try
            Dim index As Integer = GridView22.SelectedIndex
            If index = -1 Then
                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "clientScript", "alert('ต้องเลือกรายการที่ต้องการลบก่อน')", True)
            Else
                dtAcc22 = Session("D")
                dtAcc22.Rows.RemoveAt(index)
                GridView22.DataSource = dtAcc22
                GridView22.DataBind() : Me.lblCount_GridView22.Text = dtAcc22.Rows.Count

                Session("D") = dtAcc22
                ClearFieldD()
            End If

        Catch ex As Exception
            ScriptManager.RegisterStartupScript(Page, _
                                                            Page.GetType(), _
                                                            "clientScript", _
                                                            "alert('" + ex.Message.Replace("'", "") + "')", _
                                                            True)
        End Try
    End Sub

    Protected Sub GridView22_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView22.SelectedIndexChanged
        If GridView22.SelectedIndex = indexGridView22 Then
            ClearFieldD()
        Else
            ddlPrefixTH_D.SelectedIndex = CType(GridView22.Rows(GridView22.SelectedIndex).FindControl("lblGrid_THPREFIX_D"), Label).Text
            txtINDV_FIRST_NAME_D.Text = CType(GridView22.Rows(GridView22.SelectedIndex).FindControl("lblGrid_INDV_FIRST_NAME_D"), Label).Text
            txtINDV_LAST_NAME_D.Text = CType(GridView22.Rows(GridView22.SelectedIndex).FindControl("lblGrid_INDV_LAST_NAME_D"), Label).Text
            txtUNIQUEID_D.Text = CType(GridView22.Rows(GridView22.SelectedIndex).FindControl("lblGrid_UNIQUEID_D"), Label).Text

            hidIndexD.Value = GridView22.SelectedIndex.ToString()
            btnAddD.Visible = False
            btnEditD.Visible = True
        End If
    End Sub

    Protected Sub GridView22_SelectedIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSelectEventArgs) Handles GridView22.SelectedIndexChanging
        indexGridView22 = GridView22.SelectedIndex
    End Sub

    ' ------- ผู้ถือหุ้นบุคคลธรรมดา -------
    Protected Sub btnAddF_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAddF.Click
        Dim dRow As DataRow
        ''''''Validate before add row''''''''
        If Validate_F() = False Then Exit Sub
        ''''''''''''''''''''''''''''''''''''''
        If (Session("F") Is Nothing) Then
            dtAcc3.Columns.Add(New DataColumn("GROUP_CODE"))
            dtAcc3.Columns.Add(New DataColumn("GROUP_NAME"))
            dtAcc3.Columns.Add(New DataColumn("GROUP_CODE_SUB"))
            dtAcc3.Columns.Add(New DataColumn("GROUP_CODE_SUB_NAME"))
            dtAcc3.Columns.Add(New DataColumn("GROUP_CODE_SUB_SEQ"))
            dtAcc3.Columns.Add(New DataColumn("THPREFIX"))
            dtAcc3.Columns.Add(New DataColumn("THPREFIX_DESC"))
            dtAcc3.Columns.Add(New DataColumn("INDV_FIRST_NAME"))
            dtAcc3.Columns.Add(New DataColumn("INDV_LAST_NAME"))
            dtAcc3.Columns.Add(New DataColumn("SEARCH_RESULT"))
            dtAcc3.Columns.Add(New DataColumn("RISK_LEVEL"))
            dtAcc3.Columns.Add(New DataColumn("UNIQUEID"))
        Else
            dtAcc3 = Session("F")
        End If

        dRow = dtAcc3.NewRow()
        dRow("GROUP_CODE") = System.Convert.ToInt32(lblGroupCode_3.Text)
        dRow("GROUP_NAME") = lblGroupCodeName_3.Text
        dRow("GROUP_CODE_SUB") = System.Convert.ToInt32(lblGroupCodeSub_31.Text)
        dRow("GROUP_CODE_SUB_NAME") = Position_31
        dRow("GROUP_CODE_SUB_SEQ") = GridView3.Rows.Count + 1
        dRow("THPREFIX") = Me.ddlPrefixTH_F.SelectedIndex
        dRow("THPREFIX_DESC") = Me.ddlPrefixTH_F.SelectedItem
        dRow("INDV_FIRST_NAME") = txtINDV_FIRST_NAME_F.Text
        dRow("INDV_LAST_NAME") = txtINDV_LAST_NAME_F.Text
        dRow("SEARCH_RESULT") = Me.lblSanc_Result_F.Text
        dRow("RISK_LEVEL") = Me.lblRisk_Level_F.Text
        dRow("UNIQUEID") = txtUNIQUEID_F.Text
        dtAcc3.Rows.Add(dRow)
        dtAcc3.AcceptChanges()

        GridView3.DataSource = dtAcc3
        GridView3.DataBind()
        Me.lblCount_ShrHolder_INDV.Text = dtAcc3.Rows.Count
        hidHaveEdit.Value = "1"
        Session("F") = dtAcc3
        ClearFieldF()
    End Sub

    Protected Sub btnEditF_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnEditF.Click
        Dim index_row As Integer = System.Convert.ToInt32(hidIndexF.Value)
        dtAcc3 = Session("F")
        dtAcc3(index_row)("THPREFIX") = Me.ddlPrefixTH_F.SelectedIndex
        dtAcc3(index_row)("THPREFIX_DESC") = Me.ddlPrefixTH_F.SelectedItem
        dtAcc3(index_row)("INDV_FIRST_NAME") = txtINDV_FIRST_NAME_F.Text
        dtAcc3(index_row)("INDV_LAST_NAME") = txtINDV_LAST_NAME_F.Text
        dtAcc3(index_row)("UNIQUEID") = txtUNIQUEID_F.Text
        GridView3.DataSource = dtAcc3
        GridView3.DataBind()
        Me.lblCount_ShrHolder_INDV.Text = dtAcc3.Rows.Count

        hidHaveEdit.Value = "1"
        Session("F") = dtAcc3
        ClearFieldF()
    End Sub

    Protected Sub btnDelF_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDelF.Click
        Try
            Dim index As Integer = GridView3.SelectedIndex
            If index = -1 Then
                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "clientScript", "alert('ต้องเลือกรายการที่ต้องการลบก่อน')", True)
            Else
                dtAcc3 = Session("F")
                dtAcc3.Rows.RemoveAt(index)
                GridView3.DataSource = dtAcc3
                GridView3.DataBind()
                Me.lblCount_ShrHolder_INDV.Text = dtAcc3.Rows.Count

                Session("F") = dtAcc3
                ClearFieldF()
            End If

        Catch ex As Exception
            ScriptManager.RegisterStartupScript(Page, _
                                                            Page.GetType(), _
                                                            "clientScript", _
                                                            "alert('" + ex.Message.Replace("'", "") + "')", _
                                                            True)
        End Try
    End Sub

    Protected Sub GridView3_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView3.SelectedIndexChanged
        If GridView3.SelectedIndex = indexGridView3 Then
            ClearFieldF()
        Else
            ddlPrefixTH_F.SelectedValue = CType(GridView3.Rows(GridView3.SelectedIndex).FindControl("lblGrid_THPREFIX_F"), Label).Text
            txtINDV_FIRST_NAME_F.Text = CType(GridView3.Rows(GridView3.SelectedIndex).FindControl("lblGrid_INDV_FIRST_NAME_F"), Label).Text
            txtINDV_LAST_NAME_F.Text = CType(GridView3.Rows(GridView3.SelectedIndex).FindControl("lblGrid_INDV_LAST_NAME_F"), Label).Text
            txtUNIQUEID_F.Text = CType(GridView3.Rows(GridView3.SelectedIndex).FindControl("lblGrid_UNIQUEID_F"), Label).Text

            hidIndexF.Value = GridView3.SelectedIndex.ToString()
            btnAddF.Visible = False
            btnEditF.Visible = True
        End If
    End Sub

    Protected Sub GridView3_SelectedIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSelectEventArgs) Handles GridView3.SelectedIndexChanging
        indexGridView3 = GridView3.SelectedIndex
    End Sub

    ' ------- ผู้ถือหุ้นนิติบุคคลตั้งแต่ร้อยละ 20 ขึ้นไป -------
    Protected Sub btnAddG_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAddG.Click
        Dim dRow As DataRow

        If Validate_G() = False Then Exit Sub

        If (Session("G") Is Nothing) Then
            dtAcc4.Columns.Add(New DataColumn("GROUP_CODE"))
            dtAcc4.Columns.Add(New DataColumn("GROUP_NAME"))
            dtAcc4.Columns.Add(New DataColumn("GROUP_CODE_SUB"))
            dtAcc4.Columns.Add(New DataColumn("GROUP_CODE_SUB_NAME"))
            dtAcc4.Columns.Add(New DataColumn("GROUP_CODE_SUB_SEQ"))
            dtAcc4.Columns.Add(New DataColumn("CORP_ID_SHR_HOLDER"))
            dtAcc4.Columns.Add(New DataColumn("CORP_ID_SHR_HOLDER_NAME"))
            dtAcc4.Columns.Add(New DataColumn("SEARCH_RESULT"))
        Else
            dtAcc4 = Session("G")
        End If

        dRow = dtAcc4.NewRow()
        dRow("GROUP_CODE") = System.Convert.ToInt32(lblGroupCode_4.Text)
        dRow("GROUP_NAME") = lblGroupCodeName_4.Text
        dRow("GROUP_CODE_SUB") = System.Convert.ToInt32(lblGroupCodeSub_41.Text)
        dRow("GROUP_CODE_SUB_NAME") = Position_41
        'dRow("GROUP_CODE_SUB_SEQ") = Get_GROUP_CODE_SEQ() 'Prachak
        dRow("GROUP_CODE_SUB_SEQ") = If(GridView4.Rows.Count = 0, 1, dtAcc4(GridView4.Rows.Count - 1)("GROUP_CODE_SUB_SEQ") + 1) 'Boy
        dRow("CORP_ID_SHR_HOLDER") = txtCORP_ID_SHR_HOLDER.Text
        dRow("CORP_ID_SHR_HOLDER_NAME") = txtCORP_ID_SHR_HOLDER_NAME.Text
        dtAcc4.Rows.Add(dRow)
        dtAcc4.AcceptChanges()

        GridView4.DataSource = dtAcc4
        GridView4.DataBind() : Me.lblCount_GridView4.Text = dtAcc4.Rows.Count
        hidHaveEdit.Value = "1"
        Session("G") = dtAcc4
        ClearFieldG()
    End Sub

    Protected Sub btnEditG_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnEditG.Click
        Dim index_row As Integer = System.Convert.ToInt32(hidIndexG.Value)
        dtAcc4 = Session("G")
        dtAcc4(index_row)("CORP_ID_SHR_HOLDER") = txtCORP_ID_SHR_HOLDER.Text
        dtAcc4(index_row)("CORP_ID_SHR_HOLDER_NAME") = txtCORP_ID_SHR_HOLDER_NAME.Text
        GridView4.DataSource = dtAcc4
        GridView4.DataBind() : Me.lblCount_GridView4.Text = dtAcc4.Rows.Count

        hidHaveEdit.Value = "1"
        Session("G") = dtAcc4
        ClearFieldG()
    End Sub

    Protected Sub btnDelG_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDelG.Click
        Try
            Dim index As Integer = GridView4.SelectedIndex
            If index = -1 Then
                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "clientScript", "alert('ต้องเลือกรายการที่ต้องการลบก่อน')", True)
            Else
                dtAcc4 = Session("G")

                ' ----------------- Delete
                Dim dRow As DataRow
                If (ViewState("G_Del_level") Is Nothing) Then
                    dtAcc4_Del.Columns.Add(New DataColumn("CORP_LEVEL"))
                    dtAcc4_Del.Columns.Add(New DataColumn("CORP_SEQ"))
                    dtAcc4_Del.Columns.Add(New DataColumn("GROUP_CODE_SUB_SEQ"))
                Else
                    dtAcc4_Del = ViewState("G_Del_level")
                End If

                dRow = dtAcc4_Del.NewRow()
                dRow("CORP_LEVEL") = txtCORP_LEVEL.Text
                dRow("CORP_SEQ") = txtCORP_SEQ.Text
                dRow("GROUP_CODE_SUB_SEQ") = dtAcc4(index)("GROUP_CODE_SUB_SEQ")
                dtAcc4_Del.Rows.Add(dRow)
                dtAcc4_Del.AcceptChanges()
                ViewState("G_Del_level") = dtAcc4_Del
                ' -----------------

                dtAcc4.Rows.RemoveAt(index)
                GridView4.DataSource = dtAcc4
                GridView4.DataBind() : Me.lblCount_GridView4.Text = dtAcc4.Rows.Count

                Session("G") = dtAcc4
                ClearFieldG()
            End If

        Catch ex As Exception
            ScriptManager.RegisterStartupScript(Page, _
                                                            Page.GetType(), _
                                                            "clientScript", _
                                                            "alert('" + ex.Message.Replace("'", "") + "')", _
                                                            True)
        End Try
    End Sub

    Protected Sub btnAddSub_Corp_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAddSub_Corp.Click
        If Not lblSave.Visible And hidHaveEdit.Value = "1" Then
            ScriptManager.RegisterStartupScript(Page, Page.GetType(), "clientScript", "alert('กรุณาบันทึกข้อมูลก่อน ""เพิ่มโครงสร้างนิติบุคคล""')", True)
            Return
        End If

        Dim new_CORP_LEVEL = txtCORP_LEVEL.Text + txtCORP_SEQ.Text
        Dim new_CORP_SEQ = hidSeqG.Value
        Dim new_CORP_ID = txtCORP_ID_SHR_HOLDER.Text
        Dim new_CORP_NAME = txtCORP_ID_SHR_HOLDER_NAME.Text
        Dim new_OWNER_CORP_ID = txtCORP_ID.Text
        Dim new_OWNER_CORP_NAME = txtCORP_NAME.Text

        ''''Prachak'''''''''''''''''''''''''''''''''''''''
        Me.txtCORP_LEVEL_OLD.Text = Me.txtCORP_LEVEL.Text
        Me.txtCORP_SEQ_OLD.Text = Me.txtCORP_SEQ.Text
        ''''Prachak'''''''''''''''''''''''''''''''''''''''

        ClearPage()
        BindData(System.Convert.ToInt32(new_CORP_LEVEL), System.Convert.ToInt32(new_CORP_SEQ))

     

        txtCORP_ID.Text = new_CORP_ID
        txtCORP_NAME.Text = new_CORP_NAME
        txtOWNER_CORP_ID.Text = new_OWNER_CORP_ID
        txtOWNER_CORP_NAME.Text = new_OWNER_CORP_NAME
        txtCORP_LEVEL.Text = new_CORP_LEVEL
        txtCORP_SEQ.Text = new_CORP_SEQ
        btnBack.Visible = True

        'Session("sub_CORP_LEVEL") = txtCORP_LEVEL.Text + txtCORP_SEQ.Text
        'Session("sub_CORP_SEQ") = hidSeqG.Value
        'Response.Redirect(Request.RawUrl)

    End Sub

    Protected Sub GridView4_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView4.SelectedIndexChanged
        If GridView4.SelectedIndex = indexGridView4 Then
            ClearFieldG()
        Else
            txtCORP_ID_SHR_HOLDER.Text = CType(GridView4.Rows(GridView4.SelectedIndex).FindControl("lblGrid_CORP_ID_SHR_HOLDER"), Label).Text
            txtCORP_ID_SHR_HOLDER_NAME.Text = CType(GridView4.Rows(GridView4.SelectedIndex).FindControl("lblGrid_CORP_ID_SHR_HOLDER_NAME"), Label).Text

            hidSeqG.Value = CType(GridView4.Rows(GridView4.SelectedIndex).FindControl("lblGrid_Seq"), Label).Text
            hidIndexG.Value = GridView4.SelectedIndex.ToString()
            btnAddG.Visible = False
            btnEditG.Visible = True
            btnAddSub_Corp.Visible = True
        End If
    End Sub

    Protected Sub GridView4_SelectedIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSelectEventArgs) Handles GridView4.SelectedIndexChanging
        indexGridView4 = GridView4.SelectedIndex
    End Sub

    ' ------- ผู้รับมอบอำนาจให้สร้างความสัมพันธ์กับธนาคาร -------
    Protected Sub btnAddH_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAddH.Click
        Dim dRow As DataRow

        ''''''Validate before add row''''''''
        If Validate_H() = False Then Exit Sub
        ''''''''''''''''''''''''''''''''''''''

        If (Session("H") Is Nothing) Then
            dtAcc5.Columns.Add(New DataColumn("SEQ"))
            dtAcc5.Columns.Add(New DataColumn("GROUP_CODE"))
            dtAcc5.Columns.Add(New DataColumn("GROUP_NAME"))
            dtAcc5.Columns.Add(New DataColumn("GROUP_CODE_SUB"))
            dtAcc5.Columns.Add(New DataColumn("GROUP_CODE_SUB_NAME"))
            dtAcc5.Columns.Add(New DataColumn("GROUP_CODE_SUB_SEQ"))
            dtAcc5.Columns.Add(New DataColumn("THPREFIX"))
            dtAcc5.Columns.Add(New DataColumn("THPREFIX_DESC"))
            dtAcc5.Columns.Add(New DataColumn("INDV_FIRST_NAME"))
            dtAcc5.Columns.Add(New DataColumn("INDV_LAST_NAME"))
            dtAcc5.Columns.Add(New DataColumn("CONNECTED_TO_CORP"))
            dtAcc5.Columns.Add(New DataColumn("F_COMPLETE"))
            dtAcc5.Columns.Add(New DataColumn("SEARCH_RESULT"))
            dtAcc5.Columns.Add(New DataColumn("RISK_LEVEL"))
            dtAcc5.Columns.Add(New DataColumn("UNIQUEID"))
        Else
            dtAcc5 = Session("H")
        End If

        dRow = dtAcc5.NewRow()
        'dRow("SEQ") = GridView5.Rows.Count + 1
        dRow("GROUP_CODE") = System.Convert.ToInt32(lblGroupCode_5.Text)
        dRow("GROUP_NAME") = lblGroupCodeName_5.Text
        dRow("GROUP_CODE_SUB") = System.Convert.ToInt32(lblGroupCodeSub_51.Text)
        dRow("GROUP_CODE_SUB_NAME") = Position_51
        dRow("GROUP_CODE_SUB_SEQ") = GridView5.Rows.Count + 1
        dRow("THPREFIX") = Me.ddlPrefixTH_H.SelectedIndex
        dRow("THPREFIX_DESC") = Me.ddlPrefixTH_H.SelectedItem
        dRow("INDV_FIRST_NAME") = txtINDV_FIRST_NAME_H.Text
        dRow("INDV_LAST_NAME") = txtINDV_LAST_NAME_H.Text
        dRow("CONNECTED_TO_CORP") = Me.txtINDV_CONNECTED_TO_CORP_H.Text
        dRow("F_COMPLETE") = "N"
        dRow("SEARCH_RESULT") = Me.lblSanc_Result_H.Text
        dRow("RISK_LEVEL") = Me.lblRisk_Level_H.Text
        dRow("UNIQUEID") = txtUNIQUEID_H.Text

        dtAcc5.Rows.Add(dRow)
        dtAcc5.AcceptChanges()

        GridView5.DataSource = dtAcc5
        GridView5.DataBind() : Me.lblCount_GridView5.Text = dtAcc5.Rows.Count
        hidHaveEdit.Value = "1"
        Session("H") = dtAcc5
        ClearFieldH()
    End Sub

    Protected Sub btnEditH_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnEditH.Click
        Dim index_row As Integer = System.Convert.ToInt32(hidIndexH.Value)
        dtAcc5 = Session("H")
        dtAcc5(index_row)("THPREFIX") = Me.ddlPrefixTH_H.SelectedIndex
        dtAcc5(index_row)("THPREFIX_DESC") = Me.ddlPrefixTH_H.SelectedItem
        dtAcc5(index_row)("INDV_FIRST_NAME") = txtINDV_FIRST_NAME_H.Text
        dtAcc5(index_row)("INDV_LAST_NAME") = txtINDV_LAST_NAME_H.Text
        dtAcc5(index_row)("UNIQUEID") = txtUNIQUEID_H.Text
        GridView5.DataSource = dtAcc5
        GridView5.DataBind() : Me.lblCount_GridView5.Text = dtAcc5.Rows.Count

        hidHaveEdit.Value = "1"
        Session("H") = dtAcc5
        ClearFieldH()
    End Sub

    Protected Sub btnDelH_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDelH.Click
        Try
            Dim index As Integer = GridView5.SelectedIndex
            If index = -1 Then
                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "clientScript", "alert('ต้องเลือกรายการที่ต้องการลบก่อน')", True)
            Else
                dtAcc5 = Session("H")
                dtAcc5.Rows.RemoveAt(index)
                GridView5.DataSource = dtAcc5
                GridView5.DataBind() : Me.lblCount_GridView5.Text = dtAcc5.Rows.Count

                Session("H") = dtAcc5
                ClearFieldH()
            End If

        Catch ex As Exception
            ScriptManager.RegisterStartupScript(Page, _
                                                            Page.GetType(), _
                                                            "clientScript", _
                                                            "alert('" + ex.Message.Replace("'", "") + "')", _
                                                            True)
        End Try
    End Sub

    Protected Sub GridView5_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView5.SelectedIndexChanged
        If GridView5.SelectedIndex = indexGridView5 Then
            ClearFieldH()
        Else
            ddlPrefixTH_H.SelectedIndex = CType(GridView5.Rows(GridView5.SelectedIndex).FindControl("lblGrid_THPREFIX_H"), Label).Text
            txtINDV_FIRST_NAME_H.Text = CType(GridView5.Rows(GridView5.SelectedIndex).FindControl("lblGrid_INDV_FIRST_NAME_H"), Label).Text
            txtINDV_LAST_NAME_H.Text = CType(GridView5.Rows(GridView5.SelectedIndex).FindControl("lblGrid_INDV_LAST_NAME_H"), Label).Text
            txtUNIQUEID_H.Text = CType(GridView5.Rows(GridView5.SelectedIndex).FindControl("lblGrid_UNIQUEID_H"), Label).Text
            txtINDV_CONNECTED_TO_CORP_H.Text = CType(GridView5.Rows(GridView5.SelectedIndex).FindControl("lblGrid_CONNECTED_TO_CORP_H"), Label).Text
            txtINDV_COMPLETED_H.Text = CType(GridView5.Rows(GridView5.SelectedIndex).FindControl("lblGrid_F_COMPLETE_H"), Label).Text

            hidIndexH.Value = GridView5.SelectedIndex.ToString()
            btnAddH.Visible = False
            btnEditH.Visible = True

        End If
    End Sub

    Protected Sub GridView5_SelectedIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSelectEventArgs) Handles GridView5.SelectedIndexChanging
        indexGridView5 = GridView5.SelectedIndex
    End Sub

    ' ------- Main Button(SavePopup) -------
    Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Dim count_data = GridView1.Rows.Count + GridView21.Rows.Count + GridView22.Rows.Count + GridView3.Rows.Count + GridView4.Rows.Count + GridView5.Rows.Count
        Dim msg = "alert('กรุณากรอกรายละเอียด ข้อมูลโครงสร้งการบริหารจัดการหรือการเป็นเจ้าของ');"
        If count_data = 0 Then
            ScriptManager.RegisterStartupScript(Page, Page.GetType(), "clientScript", msg, True)
            Return
        End If

        SavePopup()
    End Sub

    Protected Sub btnBack_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnBack.Click
        Dim new_CORP_LEVEL = txtCORP_LEVEL.Text.Substring(0, txtCORP_LEVEL.Text.Length - 1)
        Dim new_CORP_SEQ = txtCORP_LEVEL.Text.Substring(txtCORP_LEVEL.Text.Length - 1, 1)

        ClearPage()

        txtCORP_LEVEL.Text = new_CORP_LEVEL
        txtCORP_SEQ.Text = new_CORP_SEQ

        ''''Prachak''''''''''''''''''''''
        txtCORP_LEVEL.Text = Me.txtCORP_LEVEL_OLD.Text : Me.txtCORP_LEVEL_OLD.Text = ""
        txtCORP_SEQ.Text = Me.txtCORP_SEQ_OLD.Text : Me.txtCORP_SEQ_OLD.Text = ""
        ''''Prachak''''''''''''''''''''''

        BindData(System.Convert.ToInt32(new_CORP_LEVEL), System.Convert.ToInt32(new_CORP_SEQ))

        ' ซ่อนปุ่ม back กรณีย้อนกลับไปถึงโหนดหัวสุด
        If txtCORP_LEVEL.Text = 1 And txtCORP_SEQ.Text = 0 Then
            btnBack.Visible = False
        End If
    End Sub

    ' ------- Methods -------
    Private Sub Set_StartPage()
        hidHaveEdit.Value = "0"
        hidCUSTSEQ.Value = Request.QueryString("CustSeq")
        hidCUSTTYPE.Value = Request.QueryString("CustType")
        txtCORP_ID.Text = Request.QueryString("BIZCODE")
        txtCORP_NAME.Text = Request.QueryString("NAMETH")
        txtOWNER_CORP_ID.Text = Request.QueryString("BIZCODE")
        txtOWNER_CORP_NAME.Text = Request.QueryString("NAMETH")

        txtCORP_LEVEL.Text = "1"
        txtCORP_SEQ.Text = "0"

        Session("A") = Nothing
        Session("S") = Nothing
        Session("D") = Nothing
        Session("F") = Nothing
        Session("G") = Nothing
        Session("H") = Nothing

        GridView1.DataSource = dtAcc1
        GridView1.DataBind() : Me.lblCount_GridView1.Text = dtAcc1.Rows.Count
        GridView21.DataSource = dtAcc21
        GridView21.DataBind() : Me.lblCount_GridView21.Text = dtAcc21.Rows.Count
        GridView22.DataSource = dtAcc22
        GridView22.DataBind() : Me.lblCount_GridView22.Text = dtAcc22.Rows.Count
        GridView3.DataSource = dtAcc3
        GridView3.DataBind() : Me.lblCount_ShrHolder_INDV.Text = dtAcc3.Rows.Count
        GridView4.DataSource = dtAcc4
        GridView4.DataBind() : Me.lblCount_GridView4.Text = dtAcc4.Rows.Count
        GridView5.DataSource = dtAcc5
        GridView5.DataBind() : Me.lblCount_GridView5.Text = dtAcc5.Rows.Count
    End Sub

    Private Sub BindData(Optional ByVal CORP_LEVEL As Integer = -1, Optional ByVal CORP_SEQ As Integer = -1)
        Dim dt_all = SearchData(hidCUSTSEQ.Value, CORP_LEVEL, CORP_SEQ)
        If dt_all.Rows.Count > 0 Then
            '' ------ ข้อมูล Master (เลขทะเบียนนิติบุคคล, ชื่อนิติบุคคล, เลขทะเบียนนิติบุคคลที่เป็นเจ้าของ, ชื่อนิติบุคคลที่เป็นเจ้าของ)
            txtCORP_ID.Text = dt_all(0)("CORP_ID").ToString()
            txtCORP_NAME.Text = dt_all(0)("CORP_NAME").ToString()
            txtOWNER_CORP_ID.Text = dt_all(0)("OWNER_CORP_ID").ToString()
            txtOWNER_CORP_NAME.Text = dt_all(0)("OWNER_CORP_NAME").ToString()

            ' รายชื่อเจ้าของ
            Dim dt1 = From group1 In dt_all _
                        Where group1("GROUP_CODE") = 1 And group1("GROUP_CODE_SUB") = 1
                        Order By group1("GROUP_CODE_SUB_SEQ") Ascending
                        Select group1

            If dt1.Count() > 0 Then
                GridView1.DataSource = dt1.CopyToDataTable()
                Session("A") = dt1.CopyToDataTable()
            Else
                GridView1.DataSource = New DataTable()
            End If
            GridView1.DataBind()
            Me.lblCount_GridView1.Text = dt1.Count

            '' ------ Bind Data รายชื่อกรรมการ ------
            ' รายชื่อกรรมการทั้งหมด 
            Dim dt2_1 = From group1 In dt_all _
                        Where group1("GROUP_CODE") = 2 And group1("GROUP_CODE_SUB") = 1
                        Order By group1("GROUP_CODE_SUB_SEQ") Ascending
                        Select group1

            If dt2_1.Count() > 0 Then
                GridView21.DataSource = dt2_1.CopyToDataTable()
                Session("S") = dt2_1.CopyToDataTable()
            Else
                GridView21.DataSource = New DataTable()
            End If
            GridView21.DataBind()
            Me.lblCount_GridView21.Text = dt2_1.Count

            ' รายชื่อกรรมการที่มีอำนาจลงนามผูกพันนิติบุคคล
            Dim dt2_2 = From group1 In dt_all _
                        Where group1("GROUP_CODE") = 2 And group1("GROUP_CODE_SUB") = 2
                        Order By group1("GROUP_CODE_SUB_SEQ") Ascending
                        Select group1

            If dt2_2.Count() > 0 Then
                GridView22.DataSource = dt2_2.CopyToDataTable()
                Session("D") = dt2_2.CopyToDataTable()
            Else
                GridView22.DataSource = New DataTable()
            End If
            GridView22.DataBind() : Me.lblCount_GridView22.Text = dt2_2.Count

            ' ผู้ถือหุ้นบุคคลธรรมดา
            Dim dt3 = From group1 In dt_all.AsEnumerable() _
                        Where group1("GROUP_CODE") = 3 And group1("GROUP_CODE_SUB") = 1
                        Order By group1("GROUP_CODE_SUB_SEQ") Ascending
                        Select group1

            If dt3.Count() > 0 Then
                GridView3.DataSource = dt3.CopyToDataTable()
                Session("F") = dt3.CopyToDataTable()
            Else
                GridView3.DataSource = New DataTable()
            End If
            GridView3.DataBind()
            Me.lblCount_ShrHolder_INDV.Text = dt3.Count

            ' ผู้ถือหุ้นนิติบุคคล
            Dim dt4 = From group1 In dt_all _
                        Where group1("GROUP_CODE") = 4 And group1("GROUP_CODE_SUB") = 1
                        Order By group1("GROUP_CODE_SUB_SEQ") Ascending
                        Select group1

            If dt4.Count() > 0 Then
                GridView4.DataSource = dt4.CopyToDataTable()
                Session("G") = dt4.CopyToDataTable()
            Else
                GridView4.DataSource = New DataTable()
            End If
            GridView4.DataBind() : Me.lblCount_GridView4.Text = dt4.Count


            ' ผู้รับมอบอำนาจให้สร้างความสัมพันธ์กับธนาคาร
            Dim dt5 = From group1 In dt_all _
                        Where group1("GROUP_CODE") = 5 And group1("GROUP_CODE_SUB") = 1
                        Order By group1("GROUP_CODE_SUB_SEQ") Ascending
                        Select group1

            If dt5.Count() > 0 Then
                GridView5.DataSource = dt5.CopyToDataTable()
                Session("H") = dt5.CopyToDataTable()
            Else
                GridView5.DataSource = New DataTable()
            End If
            GridView5.DataBind() : Me.lblCount_GridView5.Text = dt5.Count
        Else
            txtCORP_ID.Text = Request.QueryString("BIZCODE")
            txtCORP_NAME.Text = Request.QueryString("NAMETH")
            txtOWNER_CORP_ID.Text = Request.QueryString("BIZCODE")
            txtOWNER_CORP_NAME.Text = Request.QueryString("NAMETH")

            'txtCORP_ID.Text = dt_all(0)("CORP_ID").ToString()
            'txtCORP_NAME.Text = dt_all(0)("CORP_NAME").ToString()
            'txtOWNER_CORP_ID.Text = dt_all(0)("OWNER_CORP_ID").ToString()
            'txtOWNER_CORP_NAME.Text = dt_all(0)("OWNER_CORP_NAME").ToString()
        End If
    End Sub

    Private Function SearchData(ByVal CUSTSEQ As String, Optional ByVal CORP_LEVEL As Integer = -1, Optional ByVal CORP_SEQ As Integer = -1) As DataTable
        Dim dt As New DataTable
        Dim clsVariable As clsVariable
        Dim cltParameter As New Collection
        Dim dtAdapter As New OleDbDataAdapter

        Try
            Dim strSql As String = "select * from kycadmin.MCUST_CORP_CONC "
            strSql += "where CUSTSEQ = :CUSTSEQ "

            clsVariable = New clsVariable
            clsVariable.nameColumn = "CUSTSEQ"
            clsVariable.valueColumn = CUSTSEQ
            cltParameter.Add(clsVariable)

            If CORP_LEVEL <> -1 Then
                strSql += "and CORP_LEVEL = :CORP_LEVEL "
                clsVariable = New clsVariable
                clsVariable.nameColumn = "CORP_LEVEL"
                clsVariable.valueColumn = CORP_LEVEL
                cltParameter.Add(clsVariable)
            End If

            If CORP_SEQ <> -1 Then
                strSql += "and CORP_SEQ = :CORP_SEQ"
                clsVariable = New clsVariable
                clsVariable.nameColumn = "CORP_SEQ"
                clsVariable.valueColumn = CORP_SEQ
                cltParameter.Add(clsVariable)
            End If

            dt = clsDb.QueryDataTableAddParameter(strSql, cltParameter)
            cltParameter.Clear()

        Catch ex As Exception
            Throw ex
        End Try

        Return dt

    End Function

    Private Sub SavePopup()
        Dim cltParameter As New Collection
        Dim lst As New List(Of MCUST_CORP_CONC)
        Dim success As Boolean = False
        Dim strSql As String = ""
        Dim strSql2 As String = ""
        Dim sERROR As String = ""
        Dim seq = 1

        Try
            clsDb.TransStart()

            ' Delete All by CUSTSEQ
            strSql = "delete from kycadmin.MCUST_CORP_CONC where CUSTSEQ = :CUSTSEQ AND CORP_LEVEL = :CORP_LEVEL AND CORP_SEQ = :CORP_SEQ"
            cltParameter.Add(Add_clsVariable("CUSTSEQ", hidCUSTSEQ.Value))
            cltParameter.Add(Add_clsVariable("CORP_LEVEL", txtCORP_LEVEL.Text))
            cltParameter.Add(Add_clsVariable("CORP_SEQ", txtCORP_SEQ.Text))
            clsDb.QueryExecuteNonQueryAddParameter2(strSql, cltParameter)

            ' Delete Sub Level **Case Delete นิติบุคคล
            If (Not ViewState("G_Del_level") Is Nothing) Then
                dtAcc4_Del = ViewState("G_Del_level")
                For Each d In dtAcc4_Del.Rows
                    strSql2 = "SP_DELETE_CORP_CERN_LV"
                    cltParameter = New Collection
                    cltParameter.Add(Add_clsVariable("sERROR", sERROR))
                    cltParameter.Add(Add_clsVariable("CORP_LEVEL", txtCORP_LEVEL.Text))
                    cltParameter.Add(Add_clsVariable("CORP_SEQ", txtCORP_SEQ.Text))
                    cltParameter.Add(Add_clsVariable("GROUP_CODE_SUB_SEQ", d("GROUP_CODE_SUB_SEQ")))
                    clsDb.QueryExecuteNonQueryAddParameter2(strSql2, cltParameter, sERROR)
                Next
            End If

            ' ------ รายชื่อเจ้าของ 
            If Not Session("A") Is Nothing Then
                dtAcc1 = Session("A")
                For Each row As DataRow In dtAcc1.Rows
                    Dim mcc As New MCUST_CORP_CONC
                    mcc.CUSTSEQ = hidCUSTSEQ.Value
                    mcc.CUSTTYPE = hidCUSTTYPE.Value
                    mcc.CORP_ID = txtCORP_ID.Text
                    mcc.CORP_NAME = txtCORP_NAME.Text
                    mcc.OWNER_CORP_ID = txtOWNER_CORP_ID.Text
                    mcc.OWNER_CORP_NAME = txtOWNER_CORP_NAME.Text
                    mcc.CORP_LEVEL = System.Convert.ToInt32(txtCORP_LEVEL.Text)
                    mcc.CORP_SEQ = System.Convert.ToInt32(txtCORP_SEQ.Text)
                    mcc.GROUP_CODE = row("GROUP_CODE")
                    mcc.GROUP_NAME = row("GROUP_NAME")
                    mcc.GROUP_CODE_SUB = row("GROUP_CODE_SUB")
                    mcc.GROUP_CODE_SUB_NAME = row("GROUP_CODE_SUB_NAME")
                    mcc.GROUP_CODE_SUB_SEQ = seq
                    mcc.THPREFIX = row("THPREFIX").ToString()
                    mcc.THPREFIX_DESC = row("THPREFIX_DESC").ToString()
                    mcc.INDV_FIRST_NAME = row("INDV_FIRST_NAME").ToString()
                    mcc.INDV_LAST_NAME = row("INDV_LAST_NAME").ToString()
                    mcc.SEARCH_RESULT = row("SEARCH_RESULT")
                    mcc.RISK_LEVEL = row("RISK_LEVEL")
                    mcc.CREATED_DATE = DateTime.Now
                    mcc.CREATED_BY = clsSessionMenuRight.getUserName
                    mcc.UNIQUEID = row("UNIQUEID").ToString()
                    lst.Add(mcc)
                    seq = seq + 1
                Next
                seq = 1
            End If

            ' ------ รายชื่อกรรมการที่มีอำนาจลงนามผูกพันนิติบุคคล
            If Not Session("S") Is Nothing Then
                dtAcc21 = Session("S")
                For Each row As DataRow In dtAcc21.Rows
                    Dim mcc As New MCUST_CORP_CONC
                    mcc.CUSTSEQ = hidCUSTSEQ.Value
                    mcc.CUSTTYPE = hidCUSTTYPE.Value
                    mcc.CORP_ID = txtCORP_ID.Text
                    mcc.CORP_NAME = txtCORP_NAME.Text
                    mcc.OWNER_CORP_ID = txtOWNER_CORP_ID.Text
                    mcc.OWNER_CORP_NAME = txtOWNER_CORP_NAME.Text
                    mcc.CORP_LEVEL = System.Convert.ToInt32(txtCORP_LEVEL.Text)
                    mcc.CORP_SEQ = System.Convert.ToInt32(txtCORP_SEQ.Text)
                    mcc.GROUP_CODE = row("GROUP_CODE")
                    mcc.GROUP_NAME = row("GROUP_NAME")
                    mcc.GROUP_CODE_SUB = row("GROUP_CODE_SUB")
                    mcc.GROUP_CODE_SUB_NAME = row("GROUP_CODE_SUB_NAME")
                    mcc.GROUP_CODE_SUB_SEQ = seq 'row("GROUP_CODE_SUB_SEQ")
                    mcc.THPREFIX = row("THPREFIX").ToString()
                    mcc.THPREFIX_DESC = row("THPREFIX_DESC").ToString()
                    mcc.INDV_FIRST_NAME = row("INDV_FIRST_NAME").ToString()
                    mcc.INDV_LAST_NAME = row("INDV_LAST_NAME").ToString()
                    mcc.SEARCH_RESULT = row("SEARCH_RESULT")
                    mcc.RISK_LEVEL = row("RISK_LEVEL")
                    mcc.CREATED_DATE = DateTime.Now
                    mcc.CREATED_BY = clsSessionMenuRight.getUserName
                    mcc.UNIQUEID = row("UNIQUEID").ToString()
                    lst.Add(mcc)
                    seq = seq + 1
                Next
                seq = 1
            End If

            ' ------ รายชื่อกรรมการที่ไม่มีอำนาจลงนามผูกพันนิติบุคคล
            If Not Session("D") Is Nothing Then
                dtAcc22 = Session("D")
                For Each row As DataRow In dtAcc22.Rows
                    Dim mcc As New MCUST_CORP_CONC
                    mcc.CUSTSEQ = hidCUSTSEQ.Value
                    mcc.CUSTTYPE = hidCUSTTYPE.Value
                    mcc.CORP_ID = txtCORP_ID.Text
                    mcc.CORP_NAME = txtCORP_NAME.Text
                    mcc.OWNER_CORP_ID = txtOWNER_CORP_ID.Text
                    mcc.OWNER_CORP_NAME = txtOWNER_CORP_NAME.Text
                    mcc.CORP_LEVEL = System.Convert.ToInt32(txtCORP_LEVEL.Text)
                    mcc.CORP_SEQ = System.Convert.ToInt32(txtCORP_SEQ.Text)
                    mcc.GROUP_CODE = row("GROUP_CODE")
                    mcc.GROUP_NAME = row("GROUP_NAME")
                    mcc.GROUP_CODE_SUB = row("GROUP_CODE_SUB")
                    mcc.GROUP_CODE_SUB_NAME = row("GROUP_CODE_SUB_NAME")
                    mcc.GROUP_CODE_SUB_SEQ = seq 'row("GROUP_CODE_SUB_SEQ")
                    mcc.THPREFIX = row("THPREFIX").ToString()
                    mcc.THPREFIX_DESC = row("THPREFIX_DESC").ToString()
                    mcc.INDV_FIRST_NAME = row("INDV_FIRST_NAME").ToString()
                    mcc.INDV_LAST_NAME = row("INDV_LAST_NAME").ToString()
                    mcc.SEARCH_RESULT = row("SEARCH_RESULT")
                    mcc.RISK_LEVEL = row("RISK_LEVEL")
                    mcc.CREATED_DATE = DateTime.Now
                    mcc.CREATED_BY = clsSessionMenuRight.getUserName
                    mcc.UNIQUEID = row("UNIQUEID").ToString()
                    lst.Add(mcc)
                    seq = seq + 1
                Next
                seq = 1
            End If

            ' ------ ผู้ถือหุ้นบุคคลธรรมดา
            If Not Session("F") Is Nothing Then
                dtAcc3 = Session("F")
                For Each row As DataRow In dtAcc3.Rows
                    Dim mcc As New MCUST_CORP_CONC
                    mcc.CUSTSEQ = hidCUSTSEQ.Value
                    mcc.CUSTTYPE = hidCUSTTYPE.Value
                    mcc.CORP_ID = txtCORP_ID.Text
                    mcc.CORP_NAME = txtCORP_NAME.Text
                    mcc.OWNER_CORP_ID = txtOWNER_CORP_ID.Text
                    mcc.OWNER_CORP_NAME = txtOWNER_CORP_NAME.Text
                    mcc.CORP_LEVEL = System.Convert.ToInt32(txtCORP_LEVEL.Text)
                    mcc.CORP_SEQ = System.Convert.ToInt32(txtCORP_SEQ.Text)
                    mcc.GROUP_CODE = row("GROUP_CODE")
                    mcc.GROUP_NAME = row("GROUP_NAME")
                    mcc.GROUP_CODE_SUB = row("GROUP_CODE_SUB")
                    mcc.GROUP_CODE_SUB_NAME = row("GROUP_CODE_SUB_NAME")
                    mcc.GROUP_CODE_SUB_SEQ = seq 'row("GROUP_CODE_SUB_SEQ")
                    mcc.THPREFIX = row("THPREFIX").ToString()
                    mcc.THPREFIX_DESC = row("THPREFIX_DESC").ToString()
                    mcc.INDV_FIRST_NAME = row("INDV_FIRST_NAME").ToString()
                    mcc.INDV_LAST_NAME = row("INDV_LAST_NAME").ToString()
                    mcc.SEARCH_RESULT = row("SEARCH_RESULT")
                    mcc.RISK_LEVEL = row("RISK_LEVEL")
                    mcc.CREATED_DATE = DateTime.Now
                    mcc.CREATED_BY = clsSessionMenuRight.getUserName
                    mcc.UNIQUEID = row("UNIQUEID").ToString()
                    lst.Add(mcc)
                    seq = seq + 1
                Next
                seq = 1
            End If

            ' ------ ผู้ถือหุ้นนิติบุคคล
            If Not Session("G") Is Nothing Then
                dtAcc4 = Session("G")
                For Each row As DataRow In dtAcc4.Rows
                    Dim mcc As New MCUST_CORP_CONC
                    mcc.CUSTSEQ = hidCUSTSEQ.Value
                    mcc.CUSTTYPE = hidCUSTTYPE.Value
                    mcc.CORP_ID = txtCORP_ID.Text
                    mcc.CORP_NAME = txtCORP_NAME.Text
                    mcc.OWNER_CORP_ID = txtOWNER_CORP_ID.Text
                    mcc.OWNER_CORP_NAME = txtOWNER_CORP_NAME.Text
                    mcc.CORP_LEVEL = System.Convert.ToInt32(txtCORP_LEVEL.Text)
                    mcc.CORP_SEQ = System.Convert.ToInt32(txtCORP_SEQ.Text)
                    mcc.GROUP_CODE = row("GROUP_CODE")
                    mcc.GROUP_NAME = row("GROUP_NAME")
                    mcc.GROUP_CODE_SUB = row("GROUP_CODE_SUB")
                    mcc.GROUP_CODE_SUB_NAME = row("GROUP_CODE_SUB_NAME")
                    mcc.GROUP_CODE_SUB_SEQ = row("GROUP_CODE_SUB_SEQ")
                    mcc.CORP_ID_SHR_HOLDER = row("CORP_ID_SHR_HOLDER").ToString()
                    mcc.CORP_ID_SHR_HOLDER_NAME = row("CORP_ID_SHR_HOLDER_NAME").ToString()
                    mcc.SEARCH_RESULT = ""
                    mcc.RISK_LEVEL = 0
                    mcc.CREATED_DATE = DateTime.Now
                    mcc.CREATED_BY = clsSessionMenuRight.getUserName
                    mcc.UNIQUEID = ""
                    lst.Add(mcc)
                    seq = seq + 1
                Next
                seq = 1
            End If

            ''''''ผู้รับมอบอำนาจให้สร้างความสัมพันธ์กับธนาคาร
            If Not Session("H") Is Nothing Then
                dtAcc5 = Session("H")
                For Each row As DataRow In dtAcc5.Rows
                    Dim mcc As New MCUST_CORP_CONC
                    mcc.CUSTSEQ = hidCUSTSEQ.Value
                    mcc.CUSTTYPE = hidCUSTTYPE.Value
                    mcc.CORP_ID = txtCORP_ID.Text
                    mcc.CORP_NAME = txtCORP_NAME.Text
                    mcc.OWNER_CORP_ID = txtOWNER_CORP_ID.Text
                    mcc.OWNER_CORP_NAME = txtOWNER_CORP_NAME.Text
                    mcc.CORP_LEVEL = System.Convert.ToInt32(txtCORP_LEVEL.Text)
                    mcc.CORP_SEQ = System.Convert.ToInt32(txtCORP_SEQ.Text)
                    mcc.GROUP_CODE = row("GROUP_CODE")
                    mcc.GROUP_NAME = row("GROUP_NAME")
                    mcc.GROUP_CODE_SUB = row("GROUP_CODE_SUB")
                    mcc.GROUP_CODE_SUB_NAME = row("GROUP_CODE_SUB_NAME")
                    mcc.GROUP_CODE_SUB_SEQ = seq
                    mcc.THPREFIX = row("THPREFIX").ToString()
                    mcc.THPREFIX_DESC = row("THPREFIX_DESC").ToString()
                    mcc.INDV_FIRST_NAME = row("INDV_FIRST_NAME").ToString()
                    mcc.INDV_LAST_NAME = row("INDV_LAST_NAME").ToString()
                    'mcc.CONNECTED_TO_CORP = row("CONNECTED_TO_CORP")
                    'mcc.F_COMPLETE = row("F_COMPLETE")
                    mcc.SEARCH_RESULT = row("SEARCH_RESULT")
                    mcc.RISK_LEVEL = row("RISK_LEVEL")
                    mcc.CREATED_DATE = DateTime.Now
                    mcc.CREATED_BY = clsSessionMenuRight.getUserName
                    mcc.UNIQUEID = row("UNIQUEID").ToString()
                    lst.Add(mcc)
                    seq = seq + 1
                Next
                seq = 1
            End If

            If Insert_MCUST_CORP_CONC(lst) Then
                clsDb.TransCommit()
                lblSave.Visible = True
                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "clientScript", "alert('บันทึกข้อมูลเรียบร้อยแล้ว')", True)
            Else
                lblSave.Visible = False
                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "clientScript", "alert('เกิดข้อผิดพลาด ไม่สามารถบันทึกข้อมูลได้')", True)
                clsDb.TransRollBack()
            End If
        Catch ex As Exception
            lblSave.Visible = False
            clsDb.TransRollBack()
        End Try

    End Sub

    Private Function Insert_MCUST_CORP_CONC(ByVal lst As List(Of MCUST_CORP_CONC)) As Boolean
        Dim sucess = True
        Dim cltParameter As New Collection

        Try
            For Each mcc As MCUST_CORP_CONC In lst
                Dim strSql As String = str_Insert_MCUST_CORP_CONC(mcc)
                cltParameter.Add(Add_clsVariable("CUSTSEQ", mcc.CUSTSEQ))
                cltParameter.Add(Add_clsVariable("CUSTTYPE", mcc.CUSTTYPE))
                cltParameter.Add(Add_clsVariable("CORP_ID", mcc.CORP_ID))
                cltParameter.Add(Add_clsVariable("CORP_NAME", mcc.CORP_NAME))
                cltParameter.Add(Add_clsVariable("OWNER_CORP_ID", mcc.OWNER_CORP_ID))
                cltParameter.Add(Add_clsVariable("OWNER_CORP_NAME", mcc.OWNER_CORP_NAME))
                cltParameter.Add(Add_clsVariable("CORP_LEVEL", mcc.CORP_LEVEL))
                cltParameter.Add(Add_clsVariable("CORP_SEQ", mcc.CORP_SEQ))
                cltParameter.Add(Add_clsVariable("GROUP_CODE", mcc.GROUP_CODE))
                cltParameter.Add(Add_clsVariable("GROUP_NAME", mcc.GROUP_NAME))
                cltParameter.Add(Add_clsVariable("GROUP_CODE_SUB", mcc.GROUP_CODE_SUB))
                cltParameter.Add(Add_clsVariable("GROUP_CODE_SUB_NAME", mcc.GROUP_CODE_SUB_NAME))
                cltParameter.Add(Add_clsVariable("GROUP_CODE_SUB_SEQ", mcc.GROUP_CODE_SUB_SEQ))
                cltParameter.Add(Add_clsVariable("CORP_ID_SHR_HOLDER", mcc.CORP_ID_SHR_HOLDER))
                cltParameter.Add(Add_clsVariable("CORP_ID_SHR_HOLDER_NAME", mcc.CORP_ID_SHR_HOLDER_NAME))
                cltParameter.Add(Add_clsVariable("THPREFIX", mcc.THPREFIX))
                cltParameter.Add(Add_clsVariable("THPREFIX_DESC", mcc.THPREFIX_DESC))
                cltParameter.Add(Add_clsVariable("INDV_FIRST_NAME", mcc.INDV_FIRST_NAME))
                cltParameter.Add(Add_clsVariable("INDV_LAST_NAME", mcc.INDV_LAST_NAME))
                cltParameter.Add(Add_clsVariable("CONNECTED_TO_CORP", mcc.CONNECTED_TO_CORP))
                cltParameter.Add(Add_clsVariable("F_COMPLETE", mcc.F_COMPLETE))
                cltParameter.Add(Add_clsVariable("SEARCH_RESULT", mcc.SEARCH_RESULT))
                cltParameter.Add(Add_clsVariable("RISK_LEVEL", mcc.RISK_LEVEL))
                cltParameter.Add(Add_clsVariable("CREATED_DATE", mcc.CREATED_DATE))
                cltParameter.Add(Add_clsVariable("CREATED_BY", mcc.CREATED_BY))
                cltParameter.Add(Add_clsVariable("UNIQUEID", mcc.UNIQUEID))

                sucess = clsDb.QueryExecuteNonQueryAddParameter2(strSql, cltParameter)
                If Not sucess Then
                    Exit For
                End If
                cltParameter = New Collection
            Next

            If sucess Then
                Return True
            Else
                Return False
            End If
        Catch ex As Exception
            Return False
        End Try

    End Function

    Function str_Insert_MCUST_CORP_CONC(ByVal mcc As MCUST_CORP_CONC) As String
        Dim _sql = ""
        _sql += "insert into kycadmin.MCUST_CORP_CONC ("
        _sql += "CUSTSEQ, CUSTTYPE, CORP_ID, CORP_NAME, OWNER_CORP_ID, OWNER_CORP_NAME, CORP_LEVEL, CORP_SEQ, GROUP_CODE, GROUP_NAME, "
        _sql += "GROUP_CODE_SUB, GROUP_CODE_SUB_NAME, GROUP_CODE_SUB_SEQ, CORP_ID_SHR_HOLDER, CORP_ID_SHR_HOLDER_NAME, THPREFIX, THPREFIX_DESC, "
        _sql += "INDV_FIRST_NAME, INDV_LAST_NAME, CONNECTED_TO_CORP, F_COMPLETE, SEARCH_RESULT, RISK_LEVEL, CREATED_DATE, CREATED_BY, UNIQUEID ) "
        _sql += "values ("
        _sql += ":CUSTSEQ, :CUSTTYPE, :CORP_ID, :CORP_NAME, :OWNER_CORP_ID, :OWNER_CORP_NAME, :CORP_LEVEL, :CORP_SEQ, :GROUP_CODE, :GROUP_NAME, "
        _sql += ":GROUP_CODE_SUB, :GROUP_CODE_SUB_NAME, :GROUP_CODE_SUB_SEQ, :CORP_ID_SHR_HOLDER, :CORP_ID_SHR_HOLDER_NAME, :THPREFIX, :THPREFIX_DESC, "
        _sql += ":INDV_FIRST_NAME, :INDV_LAST_NAME, :CONNECTED_TO_CORP, :F_COMPLETE, :SEARCH_RESULT, :RISK_LEVEL, :CREATED_DATE, :CREATED_BY, :UNIQUEID )"
        str_Insert_MCUST_CORP_CONC = _sql

        Return str_Insert_MCUST_CORP_CONC
    End Function

    Function str_Del_MCUST_CORP_CONC(ByVal mcc As MCUST_CORP_CONC) As String
        Dim _sql = ""
        Dim _condition = ""
        _sql += "delete from MCUST_CORP_CONC where CUSTSEQ = :CUSTSEQ,"

        If mcc.CUSTTYPE <> Nothing Then
            _condition += " CUSTSEQ = :CUSTTYPE,"
        End If
        If mcc.CORP_LEVEL <> Nothing Then
            _condition += " CORP_LEVEL = :CORP_LEVEL,"
        End If
        If mcc.CORP_SEQ <> Nothing Then
            _condition += " CORP_SEQ = :CORP_SEQ,"
        End If
        If mcc.GROUP_CODE <> Nothing Then
            _condition += " GROUP_CODE = :GROUP_CODE,"
        End If
        If mcc.GROUP_CODE_SUB <> Nothing Then
            _condition += " GROUP_CODE_SUB = :GROUP_CODE_SUB,"
        End If
        If mcc.GROUP_CODE_SUB_SEQ <> Nothing Then
            _condition += " GROUP_CODE_SUB_SEQ = :GROUP_CODE_SUB_SEQ,"
        End If
        If mcc.UNIQUEID <> Nothing Then
            _condition += " UNIQUEID = :UNIQUEID,"
        End If

        str_Del_MCUST_CORP_CONC = _sql.Substring(0, _sql.Length - 2)

        Return str_Del_MCUST_CORP_CONC
    End Function

    Private Function Add_clsVariable(ByVal nameColumn As String, ByVal valueColumn As Object) As clsVariable
        Dim clsVariable = New clsVariable
        clsVariable.nameColumn = nameColumn
        clsVariable.valueColumn = valueColumn

        Return clsVariable
    End Function

    Private Function Get_GROUP_CODE_SEQ() As String
        Dim ds As DataSet = New DataSet()
        Dim Sql = "select " & System.Configuration.ConfigurationSettings.AppSettings("SequentMCUST_CORP_CONC_SEQ") & " from dual"
        ds.Tables.Add(clsDatabase.QueryDataSet(Sql, "CORP_SEQ").Tables("CORP_SEQ").Copy)
        Return ds.Tables("CORP_SEQ").Rows(0)("NEXTVAL")
    End Function

#Region "Misc"

    Sub Clear_Screen_S()
        Me.txtINDV_FIRST_NAME_S.Text = ""
        Me.txtINDV_LAST_NAME_S.Text = ""
        Me.lblSanc_Result_S.Text = ""
        Me.lblRisk_Level_S.Text = 0
    End Sub
    Function Validate_A() As Boolean
        Validate_A = False


        If clsValid.CheckNull(Me.txtINDV_FIRST_NAME_A) = False Then Exit Function
        If clsValid.CheckNull(Me.txtINDV_LAST_NAME_A) = False Then Exit Function
        If clsValid.CheckNull(Me.txtUNIQUEID_A) = False Then Exit Function

        If Me.Validate_ID_CARD(Me.txtUNIQUEID_A) = False Then Exit Function

        If lblSanc_Result_A.Text = "" Then
            ScriptManager.RegisterStartupScript(Page, Page.GetType(), "alertMessage", "window.alert('ต้องทำการตรวจกรองรายชือก่อน');", True)
            Exit Function
        End If

        If Find_Mcustomer_for_Coporatate_All(Me.txtUNIQUEID_A.Text, Me.txtINDV_FIRST_NAME_A, Me.txtINDV_LAST_NAME_A) = False Then Exit Function

        ''''''''''''validate ข้อมูลซ้ำ''''''''''''

        For Each rowNumber As GridViewRow In GridView1.Rows

            ''''Use Findcontrol method''''''''''''''''''''''
            Dim mGridIdCard = CType(rowNumber.FindControl("lblGrid_UNIQUEID_A"), Label).Text

            If mGridIdCard = "&nbsp;" Then mGridIdCard = ""

            If mGridIdCard.Trim = Me.txtUNIQUEID_A.Text.Trim Then
                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "alertMessage", "window.alert('ข้อมูลซ้ำ ! กรูณาป้อนเลขที่บัตรประชาชนใหม่');", True)
                txtUNIQUEID_A.Focus()
                Exit Function
            End If

        Next
        ''''''''''''''''''''''''''''''''''''''
        If Me.lblSanc_Result_A.Text = "" Then
            ScriptManager.RegisterStartupScript(Page, Page.GetType(), "alertMessage", "window.alert('กรุณาตรวจกรองรายชื่อก่อน');", True)
            Me.btnSancList_A.Focus()
            Exit Function
        End If


        Validate_A = True
    End Function
    Function Validate_S() As Boolean

        Validate_S = False
        If clsValid.CheckNull(Me.txtINDV_FIRST_NAME_S) = False Then Exit Function
        If clsValid.CheckNull(Me.txtINDV_LAST_NAME_S) = False Then Exit Function
        If clsValid.CheckNull(Me.txtUNIQUEID_S) = False Then Exit Function

        If Me.Validate_ID_CARD(Me.txtUNIQUEID_S) = False Then Exit Function


        If lblSanc_Result_S.Text = "" Then
            ScriptManager.RegisterStartupScript(Page, Page.GetType(), "alertMessage", "window.alert('ต้องทำการตรวจกรองรายชือก่อน');", True)
            Exit Function
        End If

        If Find_Mcustomer_for_Coporatate_All(Me.txtUNIQUEID_S.Text, Me.txtINDV_FIRST_NAME_S, Me.txtINDV_LAST_NAME_S) = False Then Exit Function


        ''''''''''''validate ข้อมูลซ้ำ''''''''''''
        For Each rowNumber As GridViewRow In GridView21.Rows

            ''''Use Findcontrol method''''''''''''''''''''''
            Dim mGridIdCard = CType(rowNumber.FindControl("lblGrid_UNIQUEID_S"), Label).Text

            If mGridIdCard = "&nbsp;" Then mGridIdCard = ""

            If mGridIdCard.Trim = Me.txtUNIQUEID_S.Text.Trim Then
                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "alertMessage", "window.alert('ข้อมูลซ้ำ ! กรูณาป้อนเลขที่บัตรประชาชนใหม่');", True)
                txtUNIQUEID_S.Focus()
                Exit Function
            End If

        Next
        ''''''''''''''''''''''''''''''''''''''
        If Me.lblSanc_Result_S.Text = "" Then
            ScriptManager.RegisterStartupScript(Page, Page.GetType(), "alertMessage", "window.alert('กรุณาตรวจกรองรายชื่อก่อน');", True)
            Me.btnSancList_S.Focus()
            Exit Function
        End If


        Validate_S = True
    End Function
    Function Validate_D() As Boolean
        Validate_D = False
        If clsValid.CheckNull(Me.txtINDV_FIRST_NAME_D) = False Then Exit Function
        If clsValid.CheckNull(Me.txtINDV_LAST_NAME_D) = False Then Exit Function
        If clsValid.CheckNull(Me.txtUNIQUEID_D) = False Then Exit Function

        If Me.Validate_ID_CARD(Me.txtUNIQUEID_D) = False Then Exit Function

        If lblSanc_Result_D.Text = "" Then
            ScriptManager.RegisterStartupScript(Page, Page.GetType(), "alertMessage", "window.alert('ต้องทำการตรวจกรองรายชือก่อน');", True)
            Exit Function
        End If

        If Find_Mcustomer_for_Coporatate_All(Me.txtUNIQUEID_D.Text, Me.txtINDV_FIRST_NAME_D, Me.txtINDV_LAST_NAME_D) = False Then Exit Function



        ''''''''''''validate ข้อมูลซ้ำ''''''''''''
        For Each rowNumber As GridViewRow In GridView22.Rows

            ''''Use Findcontrol method''''''''''''''''''''''
            Dim mGridIdCard = CType(rowNumber.FindControl("lblGrid_UNIQUEID_D"), Label).Text

            If mGridIdCard = "&nbsp;" Then mGridIdCard = ""

            If mGridIdCard.Trim = Me.txtUNIQUEID_D.Text.Trim Then
                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "alertMessage", "window.alert('ข้อมูลซ้ำ ! กรูณาป้อนเลขที่บัตรประชาชนใหม่');", True)
                txtUNIQUEID_D.Focus()
                Exit Function
            End If

        Next
        ''''''''''''''''''''''''''''''''''''''
        If Me.lblSanc_Result_D.Text = "" Then
            ScriptManager.RegisterStartupScript(Page, Page.GetType(), "alertMessage", "window.alert('กรุณาตรวจกรองรายชื่อก่อน');", True)
            Me.btnSancList_D.Focus()
            Exit Function
        End If

        Validate_D = True
    End Function
    Function Validate_F() As Boolean
        Validate_F = False

        If CInt(Me.lblCount_ShrHolder_INDV.Text) = 5 Then
            ScriptManager.RegisterStartupScript(Page, Page.GetType(), "alertMessage", "window.alert('ระบบอนุญาติให้ป้อนข้อมูลผู้ถือหุ้นบุุคคลธรรมดาได้ไม่เกิน 5 คน');", True)
            txtINDV_FIRST_NAME_F.Focus()
            Exit Function
        End If

        If clsValid.CheckNull(Me.txtINDV_FIRST_NAME_F) = False Then Exit Function
        If clsValid.CheckNull(Me.txtINDV_LAST_NAME_F) = False Then Exit Function
        If clsValid.CheckNull(Me.txtUNIQUEID_F) = False Then Exit Function

        If Me.Validate_ID_CARD(Me.txtUNIQUEID_F) = False Then Exit Function

        If lblSanc_Result_F.Text = "" Then
            ScriptManager.RegisterStartupScript(Page, Page.GetType(), "alertMessage", "window.alert('ต้องทำการตรวจกรองรายชือก่อน');", True)
            Exit Function
        End If

        If Find_Mcustomer_for_Coporatate_All(Me.txtUNIQUEID_F.Text, Me.txtINDV_FIRST_NAME_F, Me.txtINDV_LAST_NAME_F) = False Then Exit Function



        ''''''''''''validate ข้อมูลซ้ำ''''''''''''
        For Each rowNumber As GridViewRow In GridView3.Rows

            ''''Use Findcontrol method''''''''''''''''''''''
            Dim mGridIdCard = CType(rowNumber.FindControl("lblGrid_UNIQUEID_F"), Label).Text

            If mGridIdCard = "&nbsp;" Then mGridIdCard = ""

            If mGridIdCard.Trim = Me.txtUNIQUEID_F.Text.Trim Then
                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "alertMessage", "window.alert('ข้อมูลซ้ำ ! กรูณาป้อนเลขที่บัตรประชาชนใหม่');", True)
                txtUNIQUEID_F.Focus()
                Exit Function
            End If

        Next
        ''''''''''''''''''''''''''''''''''''''
        If Me.lblSanc_Result_F.Text = "" Then
            ScriptManager.RegisterStartupScript(Page, Page.GetType(), "alertMessage", "window.alert('กรุณาตรวจกรองรายชื่อก่อน');", True)
            Me.btnSancList_F.Focus()
            Exit Function
        End If


        Validate_F = True
    End Function
    Function Validate_G() As Boolean
        Validate_G = False

        If CInt(Me.lblCount_GridView4.Text) = 5 Then
            ScriptManager.RegisterStartupScript(Page, Page.GetType(), "alertMessage", "window.alert('ระบบอนุญาติให้ป้อนข้อมูลผู้ถือหุ้นนิติบุคคลได้ไม่เกิน 5 คน');", True)
            Me.txtCORP_ID_SHR_HOLDER.Focus()
            Exit Function
        End If
        If clsValid.CheckNull(Me.txtCORP_ID_SHR_HOLDER) = False Then Exit Function
        If clsValid.CheckNull(Me.txtCORP_ID_SHR_HOLDER_NAME) = False Then Exit Function

        Validate_G = True
    End Function
    Function Validate_H() As Boolean
        Validate_H = False
        If clsValid.CheckNull(Me.txtINDV_FIRST_NAME_H) = False Then Exit Function
        If clsValid.CheckNull(Me.txtINDV_LAST_NAME_H) = False Then Exit Function
        If clsValid.CheckNull(Me.txtUNIQUEID_H) = False Then Exit Function

        If Me.Validate_ID_CARD(Me.txtUNIQUEID_H) = False Then Exit Function


        If lblSanc_Result_H.Text = "" Then
            ScriptManager.RegisterStartupScript(Page, Page.GetType(), "alertMessage", "window.alert('ต้องทำการตรวจกรองรายชือก่อน');", True)
            Exit Function
        End If

        If Find_Mcustomer_for_Coporatate_All(Me.txtUNIQUEID_H.Text, Me.txtINDV_FIRST_NAME_H, Me.txtINDV_LAST_NAME_H) = False Then Exit Function



        ''''''''''''validate ข้อมูลซ้ำ''''''''''''
        For Each rowNumber As GridViewRow In GridView5.Rows

            ''''Use Findcontrol method''''''''''''''''''''''
            Dim mGridIdCard = CType(rowNumber.FindControl("lblGrid_UNIQUEID_H"), Label).Text

            If mGridIdCard = "&nbsp;" Then mGridIdCard = ""

            If mGridIdCard.Trim = Me.txtUNIQUEID_H.Text.Trim Then
                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "alertMessage", "window.alert('ข้อมูลซ้ำ ! กรูณาป้อนเลขที่บัตรประชาชนใหม่');", True)
                txtUNIQUEID_H.Focus()
                Exit Function
            End If

        Next
        ''''''''''''''''''''''''''''''''''''''
        If Me.lblSanc_Result_H.Text = "" Then
            ScriptManager.RegisterStartupScript(Page, Page.GetType(), "alertMessage", "window.alert('กรุณาตรวจกรองรายชื่อก่อน');", True)
            Me.btnSancList_H.Focus()
            Exit Function
        End If

        Validate_H = True
    End Function

    Protected Sub btnSancList_A_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSancList_A.Click

        Try
            Session("chkKYC") = Nothing

            Dim lvsWhatIsThis As DataSet = Session("ds")
            If Not lvsWhatIsThis Is Nothing AndAlso lvsWhatIsThis.Tables("Result").Rows.Count > 0 Then
                lblRisk_Level_A.Text = Me.hdfLevel.Value
                '''''''''''''''''''''''''''''''''''''''''''
            Else
                lblRisk_Level_A.Text = 1
            End If

            If Session("Sanction") = Nothing Then
                Me.lblSanc_Result_A.Text = "ไม่มีรายชื่อใน List"
                lblRisk_Level_A.Text = 1
            Else
                Me.lblSanc_Result_A.Text = "มีรายชื่อใน List"
                Me.lblRisk_Level_A.Text = Me.hdfLevel.Value
            End If

            Session("Sanction") = Nothing

            ''''''''''''''''''''''''''''''''''

            If Me.txtINDV_FIRST_NAME_A.Text & Me.txtINDV_LAST_NAME_A.Text & Me.txtUNIQUEID_A.Text = "" Then
                Me.lblSanc_Result_A.Visible = False
                Me.lblRisk_Level_A.Visible = False
            Else
                Me.lblSanc_Result_A.Visible = True
                Me.lblRisk_Level_A.Visible = True


            End If

        Catch ex As Exception
            Console.WriteLine("")

        End Try

    End Sub

    Protected Sub btnSancList_S_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSancList_S.Click

        Try
            Session("chkKYC") = Nothing

            Dim lvsWhatIsThis As DataSet = Session("ds")
            If Not lvsWhatIsThis Is Nothing AndAlso lvsWhatIsThis.Tables("Result").Rows.Count > 0 Then
                lblRisk_Level_S.Text = Me.hdfLevel.Value
                '''''''''''''''''''''''''''''''''''''''''''
            Else
                lblRisk_Level_S.Text = 1
            End If

            If Session("Sanction") = Nothing Then
                Me.lblSanc_Result_S.Text = "ไม่มีรายชื่อใน List"
                lblRisk_Level_S.Text = 1
            Else
                Me.lblSanc_Result_S.Text = "มีรายชื่อใน List"
                Me.lblRisk_Level_S.Text = Me.hdfLevel.Value
            End If

            Session("Sanction") = Nothing

            ''''''''''''''''''''''''''''''''''

            If Me.txtINDV_FIRST_NAME_S.Text & Me.txtINDV_LAST_NAME_S.Text & Me.txtUNIQUEID_S.Text = "" Then
                Me.lblSanc_Result_S.Visible = False
                Me.lblRisk_Level_S.Visible = False
            Else
                Me.lblSanc_Result_S.Visible = True
                Me.lblRisk_Level_S.Visible = True


            End If

        Catch ex As Exception
            Console.WriteLine("")

        End Try
    End Sub

    Protected Sub btnSancList_D_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSancList_D.Click
        Try
            Session("chkKYC") = Nothing

            Dim lvsWhatIsThis As DataSet = Session("ds")
            If Not lvsWhatIsThis Is Nothing AndAlso lvsWhatIsThis.Tables("Result").Rows.Count > 0 Then
                lblRisk_Level_D.Text = Me.hdfLevel.Value
                '''''''''''''''''''''''''''''''''''''''''''
            Else
                lblRisk_Level_D.Text = 1
            End If

            If Session("Sanction") = Nothing Then
                Me.lblSanc_Result_D.Text = "ไม่มีรายชื่อใน List"
                lblRisk_Level_D.Text = 1
            Else
                Me.lblSanc_Result_D.Text = "มีรายชื่อใน List"
                Me.lblRisk_Level_D.Text = Me.hdfLevel.Value
            End If

            Session("Sanction") = Nothing

            ''''''''''''''''''''''''''''''''''

            If Me.txtINDV_FIRST_NAME_D.Text & Me.txtINDV_LAST_NAME_D.Text & Me.txtUNIQUEID_D.Text = "" Then
                Me.lblSanc_Result_D.Visible = False
                Me.lblRisk_Level_D.Visible = False
            Else
                Me.lblSanc_Result_D.Visible = True
                Me.lblRisk_Level_D.Visible = True


            End If

        Catch ex As Exception
            Console.WriteLine("")

        End Try
    End Sub

    Protected Sub btnSancList_F_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSancList_F.Click

        Try
            Session("chkKYC") = Nothing

            Dim lvsWhatIsThis As DataSet = Session("ds")
            If Not lvsWhatIsThis Is Nothing AndAlso lvsWhatIsThis.Tables("Result").Rows.Count > 0 Then
                lblRisk_Level_F.Text = Me.hdfLevel.Value
                '''''''''''''''''''''''''''''''''''''''''''
            Else
                lblRisk_Level_F.Text = 1
            End If

            If Session("Sanction") = Nothing Then
                Me.lblSanc_Result_F.Text = "ไม่มีรายชื่อใน List"
                lblRisk_Level_F.Text = 1
            Else
                Me.lblSanc_Result_F.Text = "มีรายชื่อใน List"
                Me.lblRisk_Level_F.Text = Me.hdfLevel.Value
            End If

            Session("Sanction") = Nothing

            ''''''''''''''''''''''''''''''''''

            If Me.txtINDV_FIRST_NAME_F.Text & Me.txtINDV_LAST_NAME_F.Text & Me.txtUNIQUEID_F.Text = "" Then
                Me.lblSanc_Result_F.Visible = False
                Me.lblRisk_Level_F.Visible = False
            Else
                Me.lblSanc_Result_F.Visible = True
                Me.lblRisk_Level_F.Visible = True


            End If

        Catch ex As Exception
            Console.WriteLine("")

        End Try
    End Sub

    Protected Sub btnSancList_H_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSancList_H.Click


        Try
            Session("chkKYC") = Nothing

            Dim lvsWhatIsThis As DataSet = Session("ds")
            If Not lvsWhatIsThis Is Nothing AndAlso lvsWhatIsThis.Tables("Result").Rows.Count > 0 Then
                lblRisk_Level_H.Text = Me.hdfLevel.Value
                '''''''''''''''''''''''''''''''''''''''''''
            Else
                lblRisk_Level_H.Text = 1
            End If

            If Session("Sanction") = Nothing Then
                Me.lblSanc_Result_H.Text = "ไม่มีรายชื่อใน List"
                lblRisk_Level_H.Text = 1
            Else
                Me.lblSanc_Result_H.Text = "มีรายชื่อใน List"
                Me.lblRisk_Level_H.Text = Me.hdfLevel.Value
            End If

            Session("Sanction") = Nothing

            ''''''''''''''''''''''''''''''''''

            If Me.txtINDV_FIRST_NAME_H.Text & Me.txtINDV_LAST_NAME_H.Text & Me.txtUNIQUEID_H.Text = "" Then
                Me.lblSanc_Result_H.Visible = False
                Me.lblRisk_Level_H.Visible = False
            Else
                Me.lblSanc_Result_H.Visible = True
                Me.lblRisk_Level_H.Visible = True


            End If

        Catch ex As Exception
            Console.WriteLine("")

        End Try
    End Sub

#End Region

    Class MCUST_CORP_CONC
        Public CUSTSEQ As Integer
        Public CUSTTYPE As Integer
        Public CORP_ID As String
        Public CORP_NAME As String
        Public OWNER_CORP_ID As String
        Public OWNER_CORP_NAME As String
        Public CORP_LEVEL As Integer
        Public CORP_SEQ As Integer
        Public GROUP_CODE As Integer
        Public GROUP_NAME As String
        Public GROUP_CODE_SUB As Integer
        Public GROUP_CODE_SUB_NAME As String
        Public GROUP_CODE_SUB_SEQ As Integer
        Public CORP_ID_SHR_HOLDER As String
        Public CORP_ID_SHR_HOLDER_NAME As String
        Public THPREFIX As String
        Public THPREFIX_DESC As String
        Public INDV_FIRST_NAME As String
        Public INDV_LAST_NAME As String
        Public CONNECTED_TO_CORP As String
        Public F_COMPLETE As String
        Public SEARCH_RESULT As String
        Public RISK_LEVEL As Integer
        Public CREATED_DATE As DateTime
        Public CREATED_BY As String
        Public UNIQUEID As String
    End Class

#Region "Validate"

    Function Validate_ID_CARD(ByVal Unique_no As Object) As Boolean

        clsCheckID = New clsCheckID

        Validate_ID_CARD = False

        If clsValid.CheckNull(Unique_no) = False Then Exit Function

        If Len(Unique_no.Text) <> 13 Then
            ScriptManager.RegisterStartupScript(Page, Page.GetType(), "alertMessage", "window.alert('เลขที่บัตรประชาชนไม่ครบ 13 หลัก');", True)
            Exit Function
            Unique_no.Focus()
        End If

        '  If clsValid.CheckNumeric_Smartcard(Unique_no) = False Then Exit Function


        If Not (clsCheckID.chkIDcard(Unique_no.text)) Then
            ScriptManager.RegisterStartupScript(Page, Page.GetType(), "alertMessage", "window.alert('เลขที่บัตรประชาชนไม่ถูกต้อง');", True)
            Exit Function
            Unique_no.Focus()
        End If


        Validate_ID_CARD = True
    End Function

#End Region

#Region "Dropdown List"

    Private Sub loadDataToDropDown()
        Try
            Dim clsDropDown As New clsMasterDropDown

            ''''Prachak ''''''''''''''''
            clsDropDown.setPrefixTHToDdl(Me.ddlPrefixTH_A, 1)
            clsDropDown.setPrefixTHToDdl(Me.ddlPrefixTH_S, 1)
            clsDropDown.setPrefixTHToDdl(Me.ddlPrefixTH_D, 1)
            clsDropDown.setPrefixTHToDdl(Me.ddlPrefixTH_F, 1)
            clsDropDown.setPrefixTHToDdl(Me.ddlPrefixTH_H, 1)

            clsDropDown = Nothing
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

#End Region
  
#Region "Select Data From Mcustomer"

    Function Find_Mcustomer_for_Coporatate_All(ByVal Citizen_ID As String, ByVal NameFirst As Object, ByVal NameLast As Object) As Boolean

        Try


            Dim dtSmartCard As DataTable = New DataTable
            Dim lvsFirst_Name As String = ""
            Dim lvsLast_Name As String = ""
            Dim lvsAleart As String = ""


            Find_Mcustomer_for_Coporatate_All = False

            lvsSQL = ""
            lvsSQL += "      SELECT THFIRSTNAME,                        "
            lvsSQL += "             THMIDNAME,                          "
            lvsSQL += "             THLASTNAME,                         "
            lvsSQL += "             THPREFIX,                           "
            lvsSQL += "             ENFIRSTNAME,                        "
            lvsSQL += "             ENMIDNAME,                          "
            lvsSQL += "             ENLASTNAME,                         "
            lvsSQL += "             ENPREFIX                            "
            lvsSQL += "        FROM MCUSTOMER                           "
            lvsSQL += "       WHERE UNIQUEID = '" & Citizen_ID & "'     "
            dtSmartCard = clsDatabase.QueryDataTable(lvsSQL)

            If (dtSmartCard.Rows.Count <> 0) Then

                If Not IsDBNull(dtSmartCard.Rows(0)("THFIRSTNAME")) And Not IsDBNull(dtSmartCard.Rows(0)("THLASTNAME")) Then

                    lvsFirst_Name = dtSmartCard.Rows(0)("THFIRSTNAME")
                    lvsLast_Name = dtSmartCard.Rows(0)("THLASTNAME")

                    If (lvsFirst_Name <> NameFirst.text) Or (lvsLast_Name <> NameLast.text) Then
                        lvsAleart = "ระบบได้พบชื่อและนามสกุลจากระบบ KYC -" & lvsFirst_Name & " " & lvsLast_Name & "-ไม่ตรงกับทึ่ป้อนในหน้าจอ"
                        ScriptManager.RegisterStartupScript(Page, Page.GetType(), "alertMessage", "window.alert('" & lvsAleart & "');", True)
                        Exit Function
                    End If

                End If

            Else


            End If
            Find_Mcustomer_for_Coporatate_All = True

        Catch ex As Exception
            '  Console.WriteLine("")
            ScriptManager.RegisterStartupScript(Page, Page.GetType(),
                                    "clientScript",
                                    "alert('" + ex.Message.Replace("'", "") + "')",
                                    True)

        End Try

    End Function

    Sub Find_SmartCard_for_BO(ByVal Citizen_ID As String)

        Try



            Dim dtSmartCard As DataTable = New DataTable

            lvsSQL = ""
            lvsSQL += "     SELECT FIRST_NAME,                          "
            lvsSQL += "            LAST_NAME,                           "
            lvsSQL += "            FIRST_NAME_EN,                       "
            lvsSQL += "            LAST_NAME_EN,                        "
            lvsSQL += "            BIRTH_DATE,                          "
            lvsSQL += "            START_DATE                           "
            lvsSQL += "       FROM KYCADMIN.GHB_BORAINTERFACE           "
            lvsSQL += "      WHERE CITIZEN_ID = '" & Citizen_ID & "'    "
            dtSmartCard = clsDatabase.QueryDataTable(lvsSQL)

            If (dtSmartCard.Rows.Count <> 0) Then

                If Not IsDBNull(dtSmartCard.Rows(0)("FIRST_NAME")) Then Me.txtINDV_FIRST_NAME_A.Text = dtSmartCard.Rows(0)("FIRST_NAME")
                If Not IsDBNull(dtSmartCard.Rows(0)("LAST_NAME")) Then Me.txtINDV_LAST_NAME_A.Text = dtSmartCard.Rows(0)("LAST_NAME")

                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "alertMessage", "window.alert('ระบบได้ชื่อและนามสกุลจากระบบ Smart Card มาแสดง');", True)
            Else
                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "alertMessage", "window.alert('ไม่พบข้อมูลจาก Smart Card');", True)
                Me.ddlPrefixTH_A.Focus()
            End If


        Catch ex As Exception
            '  Console.WriteLine("")
            ScriptManager.RegisterStartupScript(Page, Page.GetType(),
                                    "clientScript",
                                    "alert('" + ex.Message.Replace("'", "") + "')",
                                    True)

        End Try

    End Sub
#End Region

End Class